getVal

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function getV(obj, path) {
      var funcBody = "var value; try { value = obj." + path + ";} catch (e) { } return value;";
      var func = new Function('obj', funcBody);
      return func(obj);
    }
   
    function getVal (obj, path) {
                var paths = String(path).replace(/\[(\d+)\]/g, '.$1').split('.'), value = obj, i, len = paths.length;
                for (i = 0; i < len; i++) {
                    if (value === null || value === undefined) break;
                    value = value[paths[i]];
                }
                return value;
            };
   
    var obj = {a: [1, 2, 3, {b: {c: [4, 5]}}]}
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
getV
getV(obj, 'a[3].d.c[1]')
pending…
getVal
getVal(obj, 'a[3].d.c[1]')
pending…

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

Compare results of other browsers

0 comments

Add a comment