var arr = [];
var classInstances = [];
var functionInstances = [];
var numInstances = 100000;
function addItem(item) {
arr.push(item);
};
class TestClass {
constructor() {
}
addItem(item){
this.item = item;
arr.push(this.item)
}
}
function TestFunction() {
}
TestFunction.prototype.addItem = function(item) {
this.item = item;
arr.push(item)
}
for( var i = 0; i < numInstances; i++ ) {
classInstances[i] = new TestClass();
functionInstances[i] = new TestFunction();
}
for (var i = 0; i < numInstances; i++) {
addItem(i);
}
for (var i = 0; i < numInstances; i++) {
classInstances[i].addItem(i);
}
for (var i = 0; i < numInstances; i++) {
functionInstances[i].addItem(i);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
pure function | |
class method | |
function method |
Test name | Executions per second |
---|---|
pure function | 382.6 Ops/sec |
class method | 168.2 Ops/sec |
function method | 11.4 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and analyzed.
Benchmark Overview
The benchmark measures the performance of three different approaches: pure function calls, class methods, and new function creations (i.e., function expressions). The goal is to determine which approach is the fastest in terms of execution frequency per second.
Script Preparation Code
The script preparation code creates an array arr
and initializes several variables:
classInstances
: an array to store instances of the TestClass
classfunctionInstances
: an array to store instances of the TestFunction
functionnumInstances
: a constant set to 100,000The script also defines two functions: addItem
, which adds an item to the arr
array, and TestClass
constructor, which initializes a new instance of the class.
Individual Test Cases
There are three individual test cases:
addItem
function directly without creating an instance.for (var i = 0; i < numInstances; i++) {
addItem(i);
}
TestClass
class and calls its addItem
method.for (var i = 0; i < numInstances; i++) {
classInstances[i].addItem(i);
}
TestFunction
function and calls its addItem
method.for (var i = 0; i < numInstances; i++) {
functionInstances[i].addItem(i);
}
Library Used: TestClass
The TestClass
is a simple class with a constructor that does nothing. The addItem
method pushes an item to the arr
array and updates the instance's property.
Alternative Approaches
In JavaScript, there are alternative approaches to creating functions:
function
keyword, you can create a function expression with the var
, let
, or const
keywords.However, for this benchmark, only new function creations and class methods are being compared.
Other Considerations
Benchmarks Results
The provided benchmark results show the execution frequency per second for each test case:
Test Name | Executions Per Second |
---|---|
pure function | 82.15605163574219 |
class method | 80.90753173828125 |
function method | 79.12915802001953 |
The results suggest that the pure function approach is the fastest, followed closely by the class method and then the new function creation approach.
Keep in mind that these results may vary depending on your specific use case, hardware, and JavaScript environment.