Dereference Object Property Path From String

JavaScript performance comparison

Test case created by James Wilkins

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function index(obj, i) {
    return obj[i]
  }
  var obj = {
    a: {
      b: {
        x: 0
      }
    }
  };
  
  function deref(obj, s) {
    var i = 0;
    s = s.split('.');
    while (obj && i < s.length)
      obj = obj[s[i++]];
  }
  
  function derefrecursive(obj, s, i) {
    if (i === undefined) i = 0;
    if (i < s.length) return derefrecursive(obj[s[i]], s, i + 1);
    return obj;
  }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
split + reduce
'a.b.x'.split('.').reduce(index, obj)
pending…
eval
eval('obj.' + 'a.b.x')
pending…
deref recursive
derefrecursive(obj, 'a.b.x'.split("."))
pending…
deref loop
deref(obj, 'a.b.x')
pending…

Compare results of other browsers

Revisions

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

0 Comments