// *class* defined in terms of OLOO
var class1 = {
set_property: function(name, value) {
this.properties[name] = value;
},
get_property: function(name) {
return this.properties[name];
},
get_p: function() {
return this.p;
},
set_p: function(val) {
this.p = val;
}
}
function inst_class1() {
var obj = Object.create(class1)
obj.properties = {}
obj.p = 0;
return obj;
}
// just an object with it's own methods
function inst_class2() {
return {
properties: {},
p: 0,
set_property: function(name, value) {
this.properties[name] = value;
},
get_property: function(name) {
return this.properties[name];
},
get_p: function() {
return this.p;
},
set_p: function(val) {
this.p = val;
}
}
}
// es6 class
class class3 {
set_property(name, value) {
this.properties[name] = value;
}
get_property(name) {
return this.properties[name];
}
get_p() {
return this.p;
}
set_p(val) {
this.p = val;
}
}
function inst_class3() {
var obj3 = new class3();
obj3.properties = {};
obj3.p = 0;
return obj3
}
// the test
function test(obj) {
var result = 0;
for (var i = 0; i < N; i++) {
obj.set_property(i, i*0.33);
result += obj.get_property(i);
obj.set_p(result);
result += obj.get_p();
}
return result;
}
var N = 100000;
var result = test(inst_class1());
var result = test(inst_class2());
var result = test(inst_class3());
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
oloo | |
direct properties | |
classes |
Test name | Executions per second |
---|---|
oloo | 59.9 Ops/sec |
direct properties | 47.4 Ops/sec |
classes | 48.1 Ops/sec |
Measuring the performance of JavaScript objects and classes is crucial in understanding how different approaches impact execution speed.
Benchmark Definition JSON Explanation
The provided benchmark definition defines three test cases:
oloo
(Object Literal OOOO): This test case uses an object literal to define a class, where each property is explicitly set and retrieved using dot notation.direct properties
: This test case uses an object with its own methods, similar to the first one but without defining a class explicitly.classes
: This test case uses ES6 classes to define a class, which provides a more modern syntax for creating classes.All three test cases follow a common pattern:
inst_classX()
function.test()
function is called with this instance as an argument.test()
function performs a series of operations on the object, including setting and getting properties, and updates a property (p
) in each iteration.Comparison of Options
The three test cases differ in their approach to defining the class:
In terms of performance, the differences are likely due to:
var classX = { ... };
may have slightly slower execution compared to using class classX { ... }
.test()
function uses dot notation to access properties and methods on the object. In the oloo
test case, this involves searching through an object literal for method names, which might be slower than using a named function in other cases.Object.create()
) may have performance implications due to the overhead of creating a new object.Library/Functionality Used
The benchmark uses no external libraries or frameworks beyond standard JavaScript features.
Special JS Feature/Syntax
No special JavaScript features or syntax are used in these test cases. However, if we consider the ES6 class syntax, it's worth noting that this is a relatively modern feature introduced in ECMAScript 2015 (ES6).
Alternatives
Other alternatives for defining classes or objects with methods could include:
However, these approaches might not provide the same level of readability, maintainability, and modern syntax support offered by ES6 classes or object literals.