Catch exception

JavaScript performance comparison

Revision 7 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function getProperty(obj, prop) {
    return prop.split('.')
      .reduce(function(m, i) {
        return m && typeof m == 'object' ? m[i] : null;
      }, obj);
  }
  
  function getPropertyFast(obj, prop) {
    var parts = prop.split('.');
    for (var i = 0; i < parts.length; i++) {
      if (!obj)
        return null;
      obj = obj[parts[i]];
    }
    return obj;
  }
  
  
  var partsCache = {};
  function getPropertyFast2(obj, prop) {
    if (!partsCache.hasOwnProperty(prop)){
      partsCache[prop] = prop.split('.');
    }
    var parts = partsCache[prop];
    for (var i = 0; i < parts.length; i++) {
      if (!obj)
        return null;
      obj = obj[parts[i]];
    }
    return obj;
  }
  
  var partsCache4 = {};
  function genGetterFn4(path){
    var parts = path.split('.')
    var args = [];
    for (var i = 0; i < parts.length - 1; i++)
      args.push('n' + i);
    if (args.length)
      return new Function(args + ',last', 'return function(o){if ((o=o[' + args.join('])&&(o=o[') + ']))return o[last];}').apply(null, parts);
    else
      return function(o){
        return o[path];
      };
  }
  function getPropertyFast4(obj, parts) {
    return (partsCache4.hasOwnProperty(parts)
      ? partsCache4[parts]
      : partsCache4[parts] = genGetterFn4(parts))(obj);
  }
  
  var partsCache5 = {};
  function escapeName(name){
    return name.replace(/\\/g, '\\\\').replace(/"/g, "\"");
  }
  function genGetterFn5(path){
    var parts = path.split('.')
    var expr = [];
    for (var i = 0; i < parts.length - 1; i++)
      expr.push(escapeName(parts[i]));
    if (expr.length)
      return new Function('o', 'if ((o=o["' + expr.join('"])&&(o=o["') + '"]))return o["' +  escapeName(parts[parts.length - 1]) + '"];');
    else
      return function(o){
        return o[path];
      }
  }
  function getPropertyFast5(obj, parts) {
    return (partsCache5.hasOwnProperty(parts)
      ? partsCache5[parts]
      : partsCache5[parts] = genGetterFn5(parts))(obj);
  }
  
  // data
  var obj = {
    a: {
      b: {
        c: 42
      }
    }
  };
  var result;

};
</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
Use function to get property
result = getProperty(obj, 'a.x.y');
pending…
Use try/catch block to suppress errors
try {
  result = obj.a.x.y;
} catch (e) {}
pending…
Really fast function
result = getPropertyFast(obj, 'a.x.y');
pending…
less GC usage
result = getPropertyFast2(obj, 'a.x.y');
pending…
function gen
result = getPropertyFast4(obj, 'a.x.y');
pending…
function gen (static keys)
result = getPropertyFast5(obj, 'a.x.y');
pending…
try/catch w/o exception
try {
  result = obj.a.b.c;
} catch (e) {}
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