Get first element of Object

JavaScript performance comparison

Test case created

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var foo = {
      'key1': 'value1',
      'key2': 'value2',
      'key3': 'value3',
      'key4': 'value4',
      'key5': 'value5'
    },
        first;
};
</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(var i in foo)
for (var i in foo) {
  first = foo[i];
  break;
}
pending…
$.each
$.each(foo, function() {
  first = this;
  return false;
});
pending…
Object.keys
first = foo[Object.keys(foo)[0]];
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