'use strict';
function getInstance(param1) {
var var1 = 'hello';
var m1 = function (p) {
return 'testing: ' + p;
};
return {
getParam1: function () {
return param1;
},
var1: var1,
getResult: function (p) {
return param1 + ', ' + var1 + ', ' + m1(p);
}
};
}
var a;
var i = 0;
var t1, t2;
for (i; i < 1000; i++) {
a = getInstance('A - ABC');
t1 = a.getParam1() + a.var1;
t2 = t1 + a.getResult('result A');
}
'use strict';
var TestClass = function () {
function TestClass(param1) {
this.param1 = param1;
this.var1 = 'hello';
}
TestClass.prototype.m1 = function (p) {
return 'testing: ' + p;
};
;
TestClass.prototype.getParam1 = function () {
return this.param1;
};
TestClass.prototype.getResult = function (p) {
return this.param1 + ', ' + this.var1 + ', ' + this.m1(p);
};
return TestClass;
}();
var b;
var i = 0;
var t1, t2;
for (i; i < 1000; i++) {
b = new TestClass('B - ABC');
t1 = b.getParam1() + b.var1;
t2 = t1 + b.getResult('result B');
}
'use strict';
function getInstance(param1) {
var var1 = 'hello';
var m1 = function (p) {
return 'testing: ' + p;
};
return {
getParam1: function () {
return param1;
},
var1: var1,
getResult: function (p) {
return param1 + ', ' + var1 + ', ' + m1(p);
}
};
}
var a;
var i = 0;
var t1, t2;
a = getInstance('A - ABC');
for (i; i < 1000; i++) {
t1 = a.getParam1() + a.var1;
t2 = t1 + a.getResult('result A');
}
'use strict';
var TestClass = function () {
function TestClass(param1) {
this.param1 = param1;
this.var1 = 'hello';
}
TestClass.prototype.m1 = function (p) {
return 'testing: ' + p;
};
;
TestClass.prototype.getParam1 = function () {
return this.param1;
};
TestClass.prototype.getResult = function (p) {
return this.param1 + ', ' + this.var1 + ', ' + this.m1(p);
};
return TestClass;
}();
var b;
var i = 0;
var t1, t2;
b = new TestClass('B - ABC');
for (i; i < 1000; i++) {
t1 = b.getParam1() + b.var1;
t2 = t1 + b.getResult('result B');
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Factory | |
Class | |
Factory (init outside of loop) | |
Class (init outside of loop) |
Test name | Executions per second |
---|---|
Factory | 6546.8 Ops/sec |
Class | 5493.8 Ops/sec |
Factory (init outside of loop) | 11802.5 Ops/sec |
Class (init outside of loop) | 6966.3 Ops/sec |
I'll break down the benchmark and explain what's being tested, compared options, pros and cons, and other considerations.
Benchmark Definition The benchmark is defined by a JSON object with four properties:
Name
: The name of the benchmark (e.g., "Factory vs Class").Description
: An optional description of the benchmark.Script Preparation Code
: A code snippet that prepares the JavaScript environment for the benchmark.Html Preparation Code
: Another code snippet that may be used to prepare HTML elements.In this case, all properties are empty or null.
Individual Test Cases There are four test cases:
Factory (init inside loop)
This test case uses a factory function (getInstance
) to create an object with several methods:
getParam1
: Returns the value of param1
.var1
: A static property containing the string "hello".getResult
: Returns the concatenation of param1
, var1
, and the result of calling a method m1
with an argument.
The test case runs this factory function 1000 times in a loop, using different values for param1
.Factory (init outside of loop) This test case is similar to the previous one but initializes the object outside the loop.
Class (init inside loop)
This test case uses a class definition (TestClass
) to create instances with several methods:
getParam1
: Returns the value of this.param1
.var1
: A static property containing the string "hello".getResult
: Returns the concatenation of this.param1
, this.var1
, and the result of calling a method m1
with an argument.
The test case runs this class definition 1000 times in a loop, using different values for param1
.Class (init outside of loop) This test case is similar to the previous one but initializes the object outside the loop.
Comparison and Analysis All four test cases create objects with similar properties, but they differ in how they are created:
Factory functions create objects programmatically.
Classes define a blueprint for creating instances. The main differences between the tests are:
Initialization: Objects are created inside or outside loops.
Functionality: The factory function and class definition are used to create similar objects with different methods.
Performance Comparison The benchmark results show that the "Factory (init outside of loop)" test case outperforms all others, with an average execution rate of 6966.3251953125 executions per second. This suggests that initializing objects outside loops may be more efficient than doing so inside loops.
However, it's essential to note that this benchmark is highly dependent on the specific JavaScript engine and environment used. The results may vary significantly across different browsers, engines, or environments.
Pros and Cons
Factory functions:
Classes: