Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
oop performance test
(version: 13)
Comparing performance of:
oloo vs object with own methods(no inheritance) vs classes vs test direct properties
Created:
8 years ago
by:
Registered User
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 { constructor(props, p) { this.properties = props; this.p = p; } 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({}, 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; } // test direct properties function test1(obj) { var result = 0; for (var i = 0; i < N; i++) { obj.properties[i] = i*0.33; result += obj.properties[i]; obj.p = result; result += obj.p; } return result; } var N = 10000;
Tests:
oloo
var result = test(inst_class1());
object with own methods(no inheritance)
var result = test(inst_class2());
classes
var result = test(inst_class3());
test direct properties
var result = test1(inst_class2());
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
oloo
object with own methods(no inheritance)
classes
test direct properties
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):
I'll take a deep breath and dive into explaining what's being tested in the provided benchmark. The benchmark measures the performance of three different approaches for setting and getting properties on objects: 1. **Class-based inheritance**: `inst_class1()` creates an object that inherits from a class (`class1`) using `Object.create()`. The object has its own set of properties, which are defined inside the class. 2. **Class-based inheritance with own methods**: `inst_class2()` creates an object that also uses class-based inheritance but doesn't inherit any methods from the parent class. Instead, it defines its own methods (`set_property`, `get_property`, etc.) directly on the object. 3. **ES6 class**: `inst_class3()` creates an object using a traditional ES6 class (`class3`). This approach is similar to the first one but uses the newer syntax for defining classes. The benchmark also includes a test case (`test1`) that directly accesses properties on the object without using any class-based inheritance or ES6 features. Now, let's discuss the pros and cons of each approach: **Class-based inheritance** Pros: * Can encapsulate data and behavior in a single unit (the class) * Can use composition to create complex objects * Supports polymorphism through method overriding Cons: * May introduce additional overhead due to the creation of a new object for each instance * Requires careful consideration of inheritance hierarchies and method naming conflicts **Class-based inheritance with own methods** Pros: * Offers more flexibility than traditional class-based inheritance * Can be used when you want to inherit behavior from one class but not from another Cons: * May lead to "diamond problems" (multiple inheritance) if not handled carefully * Requires explicit method definition and resolution **ES6 class** Pros: * Simplifies the syntax for defining classes compared to traditional class-based inheritance * Offers better support for static methods, properties, and decorators * Is more concise and readable than traditional class definitions Cons: * May have slower performance due to the increased complexity of the class definition * Requires familiarity with ES6 features and syntax Other considerations: * **Direct property access**: The `test1` test case directly accesses properties on the object without using any class-based inheritance or ES6 features. This approach may be faster but also less flexible than the other options. * **Library usage**: None of the test cases use any external libraries beyond the standard JavaScript library. The benchmark results show the performance differences between these approaches, with `test1` (direct property access) being the fastest and `inst_class2` (class-based inheritance with own methods) being slower than the others. However, keep in mind that these results may vary depending on the specific use case and environment. As for other alternatives, here are a few: * **Prototypal inheritance**: Instead of using classes or ES6 features, you can create objects using prototypal inheritance by setting up a prototype chain. * **Functional programming**: You can achieve similar results using functional programming techniques, such as creating functions that return objects with desired properties. However, these alternatives may not offer the same level of encapsulation and organization as class-based inheritance or ES6 classes.
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?