[] vs Array for object conversion

JavaScript performance comparison

Test case created by FGRibreau

Info

I was wondering: “What’s the performance overhead of using [].prototype instead of Array.prototype while converting object into array?”.

It seems logic that Array.prototype will be more performant than [].prototype because [].prototype will instantiate an Array on every call.

In fact, the results are very similar and close for Firefox/Safari/Webkit. But Chrome is way more efficient with Array.prototype.

Preparation code

<script>
  unshift.apply({}, 'A B C D E F G H I J K L M N O P Q R S T U V W X Y Z'.split(' '));
</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
Array.protoype
function unshift() {
 var args = Array.prototype.slice.call(arguments);
 args.unshift(this);
}
pending…
[].prototype
function unshift() {
 var args = [].prototype.slice.call(arguments);
 args.unshift(this);
}
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