npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

perfget

v0.2.1

Published

Fastest method for returning deeply nested properties based on a string path.

Downloads

6

Readme

Build Status NPM version

perfget

Fastest method for returning deeply nested properties based on a string path.

If you merely try to access a path like a.b.c and either a or a.b does not exist, you will throw an error. If a is not defined: ReferenceError: a is not defined. If a.b is not defined: TypeError: Cannot read property 'c' of undefined.

The only safe pattern for property access is: a && a.b && a.b.c. Relying on this pattern leads to verbose, error-prone code. You will also have problems passing this around to other functions.

get will allow you to pass get( "a.b.c" ) as a path or as an array of "segments", get( ["a", "b", "c"] ). If any part of the path fails, get returns undefined.

Any properties you can normally access, get can return. For instance, this will work in the browser: get( "document.body.childNodes.length" );

Notes

No, this does not use try/catch. That tends to slow things down, especially in the browser. For each "depth", perfget constructs a function capable of accessing properties at that depth. The function gets cached for reuse. Splitting long strings can also take time, so we cache those as well for reliable, fast results.

Getting started

Download and include perfget.min.js on a browser page or install via npm:

npm install perfget --save-dev

API

  • get( path:String|Array ) : Returns property values of this.
  • _get( receiver:Object ) : Returns a function which takes a path (just like get).
  • get_( path:String|Array ): Returns a function which takes a receiver.

Uses:

Use get() globally

<!-- Browser example -->
<script src="perfget.min.js">
<script>
get( "window.location.hash" );
</script>
// NodeJS example
var get = require( "perfget" ).get;
get( "process.title" ); //'node'

Use get() for your custom objects

<!-- Browser method example -->
<script src="perfget.min.js">
<script>
function Klass( thangs ) {
  this.stuff = {
    thangs: thangs
  };
}
Klass.prototype.get = get;

var k = new Klass( [1,2,3] );
k.get( "stuff.thangs.1" ); //2
</script>
<!-- Browser Object.create example -->
<script src="perfget.min.js">
<script>
var obj = Object.create( perfget() );

obj.stuff = {
  thangs: [1,2,3]
};
obj.get( "stuff.thangs.1" ); //2
</script>
// NodeJS method example
var perfget = require( "perfget" );
function Klass( thangs ) {
  this.stuff = {
    thangs: thangs
  };
}
Klass.prototype.get = perfget.get;

var k = new Klass( [1,2,3] );
k.get( "stuff.thangs.1" ); //2
// NodeJS util.inherits example
var perfget = require( "perfget" );
var util = require( "util" );
function Klass( thangs ) {
  this.stuff = {
    thangs: thangs
  };
}
util.inherits( Klass, perfget.constructor );

var k = new Klass( [1,2,3] );
k.get( "stuff.thangs.1" ); //2
// NodeJS Object.create example
var perfget = require( "perfget" );
var obj = Object.create( perfget.factory() );
obj.stuff = {
  thangs: [1,2,3]
};

obj.get( "stuff.thangs.1" ); //2

Use _get() to wrap objects

<!-- Browser example -->
<script src="perfget.min.js">
<script>
var location = _get( window.location );
location();
location( "hash" );
</script>
// NodeJS example
var _get = require( "perfget" )._get;
var global = _get( global );
global( "process.versions.v8" );

Use get_() with promises

<!-- Browser example -->
<script src="jquery.min.js">
<script src="perfget.min.js">
<script>
var getResults = get_( "data.results" );
$.get( "http://fake.is/api" )
  .done( getResults )
  .then( handleResults )
  .fail( handleError );
</script>
// NodeJS example
var Promise = require( "es6-promise" ).Promise;
var http = require( "http" );
var get_ = require( "perfget" ).get_;
var getResults = get_( "data.results" );

function request( options, body ) {
    return new Promise( function ( resolve, reject ) {
        var req = https.request( options, resolve );
        req.on( "error", reject );
        if ( body ) {
            req.write( body );
        }
        req.end();
    } );
}

function body( res ) {
    var body = "";
    return new Promise( function ( resolve, reject ) {
        res.on( "data", function ( chunk ) {
            body += chunk;
        } );
        res.on( "end", function () {
            return (res.statusCode === 200 ? resolve : reject)( body );
        } );
    } );
}

function parse( body ) {
    return JSON.parse( body );
}

request( {
            hostname: "fake.is",
            path: "/api",
            method: "GET"
        } )
            .then( body )
            .then( parse )
            .then( getResults )
            .then( handleResults )
            .catch( handleError );

Use get_() functionally, like with .map()

<!-- Browser example -->
<script src="jquery.min.js">
<script src="perfget.min.js">
<script>
var pluckId = get_( "deeply.nested.id" );
var obj = {deeply:{nested:{id:1}}}
[obj].map( pluckId ); // [1]
</script>
// NodeJS example
var get_ = require( "perfget" ).get_;
var pluckId = get_( "deeply.nested.id" );
var obj = {deeply:{nested:{id:1}}}
[obj].map( pluckId ); // [1]

Change log

Date | Version | Notes --- | --- | --- 2014-03-24 | v0.2.1 | Added better support for Object.create 2014-03-22 | v0.2.0 | Added support for util.inherits 2014-03-18 | v0.1.1 | Replaced gulp-mocha with gulp-nodeunit, added travis-ci integration