Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
oop performance test
(version: 0)
Comparing performance of:
oloo vs direct properties vs classes
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
// *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;
Tests:
oloo
var result = test(inst_class1());
direct properties
var result = test(inst_class2());
classes
var result = test(inst_class3());
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
oloo
direct properties
classes
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. `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. 2. `direct properties`: This test case uses an object with its own methods, similar to the first one but without defining a class explicitly. 3. `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: * An instance of the defined class is created using the `inst_classX()` function. * The `test()` function is called with this instance as an argument. * The `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: 1. **oloo (Object Literal OOOO)**: * Pros: Simple and easy to understand. * Cons: May not be the most idiomatic way to define classes in JavaScript. 2. **direct properties**: * Pros: More concise than defining a separate class, but still readable. * Cons: May lead to confusion if not explicitly defined as an object with methods. 3. **classes (ES6)**: * Pros: Modern syntax for defining classes, easy to read and maintain. * Cons: Requires support for ES6 features. In terms of performance, the differences are likely due to: 1. **Class definition overhead**: Defining a class using `var classX = { ... };` may have slightly slower execution compared to using `class classX { ... }`. 2. **Method lookup**: The `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. 3. **Instance creation**: Creating an instance of a class (using `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: 1. **Protochains**: Using prototype chains to create objects and define methods. 2. **Modules**: Exporting functions as module exports to use in other scripts. However, these approaches might not provide the same level of readability, maintainability, and modern syntax support offered by ES6 classes or object literals.
Related benchmarks:
oop performance test
oop performance test
oop performance test
"this" property vs. closure upvalue
Comments
Confirm delete:
Do you really want to delete benchmark?