function vs jquery extend vs object prototype

JavaScript performance comparison

Revision 7 of this test case created by emi

Info

In FF3.6/Win, the execution part (the function calls) have nearly identical running times, so I included the function declarations themselves in the test cases.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="//base2.googlecode.com/svn/version/1.0.2/src/base2.js"></script>
<script>
extend = function(first, second){
    for (var prop in second){
        first[prop] = second[prop];
    }
};
other_object = {};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
function foo()
function aFoo() {
 var x;
 for (i = 0; i < 10; i++) {
  x = Math.random();
 }
}
aFoo();
pending…
var bar = function()
var aBar = function() {
 var x;
 for (i = 0; i < 10; i++) {
  x = Math.random();
 }
};
aBar();
pending…
$.extend
$.extend({
 aBaz: function() {
  var x;
  for (i = 0; i < 10; i++) {
   x = Math.random();
  }
 }
});
$.aBaz();
pending…
Object.prototype
Object.prototype.aQuux = function() {
 var x;
 for (i = 0; i < 10; i++) {
  x = Math.random();
 }
};
var obj = {};
obj.aQuux();
pending…
plain code
var x;
for (i = 0; i < 10; i++) {
 x = Math.random();
}
pending…
custom extend
var obj = {};
extend(obj, {
 aBaz: function() {
  var x;
  for (i = 0; i < 10; i++) {
   x = Math.random();
  }
 }
});
obj.aBaz();
pending…
Base2.extend
base2.extend({
 aBaz: function() {
  var x;
  for (i = 0; i < 10; i++) {
   x = Math.random();
  }
 }
});
base2.aBaz();
pending…
$.extend on other object than $
$.extend(other_object,{myBar: function(){
var x;
for (i = 0; i < 10; i++) {
 x = Math.random();
}
}});
other_object.myBar();
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