String dot namespace to Object (v2)

Revision 2 of this benchmark created on


Setup

var ns = function(path, context) {
      if (!context) context = global || window;
      if (!path) return context;
      if (path.length && path[path.length - 1] !== '.') path = path + '.';
      var current = context;
      var buff = "";
      for (var i = 0, len = path.length; i < len; i++) {
        if (path[i] == '.') {
          current = context[buff];
          if (!current) return context;
          context = current;
          buff = '';
        } else {
          buff += path[i];
        }
      }
      return context;
    }
    
    var ns2 = function(path, context) {
      if (!context) context = global || window;
      if (!path) return context;
      var current = context;
      var arr = path.split('.');
      var buff;
      while (buff = arr.shift()) {
        current = context[buff];
        if (!current) return context;
        context = current;
      }
      return current;
    }
    
    
    var ns3 = function(path, context) {
      if (!context) context = global || window;
      if (!path) return context;
      var current = context;
      var arr = path.split('.');
      var buff;
      for (var i = 0; i < arr.length; i++) {
        current = context[arr[i]];
        if (!current) return context;
        context = current;
      }
      return current;
    }
    
    
    var myJson = {
      "id": 0,
      "guid": "3571057d-a5c6-40e3-af2c-f4e7fe1bca87",
      "isActive": false,
      "balance": "$1,930.85",
      "picture": "http://placehold.it/32x32",
      "age": 29,
      "eyeColor": "green",
      "name": "Tate Hoover",
      "gender": "male",
      "company": "ZEROLOGY",
      "email": "tatehoover@zerology.com",
      "phone": "+1 (800) 412-3642",
      "address": "542 Chase Court, Holtville, Washington, 3766",
      "about": "Nostrud enim in incididunt ipsum anim eu dolor exercitation ex exercitation. In labore sunt commodo consequat voluptate consequat excepteur anim velit fugiat occaecat ex reprehenderit. Aliquip Lorem sit sit consequat.\r\n",
      "registered": "2014-02-01T03:36:33 -01:00",
      "latitude": -2.260617,
      "longitude": 97.380953,
      "tags": [
        "dolore",
        "mollit",
        "minim",
        "ad",
        "commodo",
        "id",
        "aliqua"
      ],
      "friends0": {
        "id": 0,
        "name": "Elsie Paul",
        "gender": "female",
        "company": "GLUID",
        "email": "elsiepaul@gluid.com",
        "phone": "+1 (932) 403-3585",
        "friends1": {
          "id": 0,
          "name": "Caroline Booth",
          "gender": "female",
          "company": "QUILITY",
          "email": "carolinebooth@quility.com",
          "phone": "+1 (843) 461-3633",
          "friends2": {
            "id": 0,
            "name": "Santana Wong",
            "gender": "male",
            "company": "SONGBIRD",
            "email": "santanawong@songbird.com",
            "phone": "+1 (812) 477-3307",
            "friends3": {
              "id": 0,
              "name": "Adele Justice",
              "gender": "female",
              "company": "ZILLA",
              "email": "adelejustice@zilla.com",
              "phone": "+1 (869) 479-3257",
              "friends4": {
                "id": 0,
                "name": "Richmond Hayes",
                "gender": "male",
                "company": "DUFLEX",
                "email": "richmondhayes@duflex.com",
                "phone": "+1 (997) 434-3774",
                "friends5": {
                  "id": 0,
                  "name": "Letha Ellison",
                  "gender": "female",
                  "company": "ZOARERE",
                  "email": "lethaellison@zoarere.com",
                  "phone": "+1 (962) 441-3223",
                  "friends6": {
                    "id": 0,
                    "name": "Phelps Skinner",
                    "gender": "male",
                    "company": "COGNICODE",
                    "email": "phelpsskinner@cognicode.com",
                    "phone": "+1 (966) 413-2566",
                    "friends7": {
                      "id": 0,
                      "name": "Pickett Medina",
                      "gender": "male",
                      "company": "MOLTONIC",
                      "email": "pickettmedina@moltonic.com",
                      "phone": "+1 (910) 439-2760",
                      "friends8": {
                        "id": 0,
                        "name": "Felicia Jensen",
                        "gender": "female",
                        "company": "INDEXIA",
                        "email": "feliciajensen@indexia.com",
                        "phone": "+1 (878) 495-2602"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      "greeting": "Hello, Tate Hoover! You have 4 unread messages.",
      "favoriteFruit": "apple"
    };

Test runner

Ready to run.

Testing in
TestOps/sec
with String loop
ns('friends0.friends1.friends2.friends3.friends4.friends5.friends6.friends7.friends8.name', myJson)
ready
spliting and shifting array
ns2('friends0.friends1.friends2.friends3.friends4.friends5.friends6.friends7.friends8.name', myJson)
ready
spliting and looping array
ns3('friends0.friends1.friends2.friends3.friends4.friends5.friends6.friends7.friends8.name', myJson)
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.