object-looping

JavaScript performance comparison

Test case created by everywhile

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var obj = {
      a: 1,
      b: 2,
      c: 3,
      d: 4,
      f: 5
    },
    i = 0,
    _hasOwnProperty = Object.prototype.hasOwnProperty,
    value = null,
    keys = Object.keys(obj)
   
    function each (obj, fn) {
            for (var key in obj) {
                if (_hasOwnProperty.call(obj, key)){
                    fn.call(obj, key, obj[key])
                }
            }
        }
   
    function each2 (obj, fn) {
        var keys = Object.keys(obj)
        for (var i=0, key; key = keys[i++]; fn.call(obj, key, obj[key]));
    }
};
</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
for in, if
for (key in obj) {
  if (_hasOwnProperty.call(obj, key)){
     value = obj[key]
  }
}
pending…
for in, continue
for (key in obj) {
  if (!_hasOwnProperty.call(obj, key)) continue;
  value = obj[key]
}
pending…
Object.keys()
Object.keys(obj).forEach(function(key){
   value = obj[key]
})
pending…
Object.keys() cached
keys.forEach(function(key){
   value = obj[key]
})
pending…
Keys for;
for (var keys = Object.keys(obj), i = 0, ln = keys.length; i < ln; i++){
    value = obj[key]
}
pending…
each, for in
each(obj, function(key, value){
})
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. Here’s a list of current revisions for this page:

0 comments

Add a comment