Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
FP vs OOP vs fake OOP 3
(version: 2)
Comparing performance of:
OOP vs FP vs Fake OOP vs Fake OOP2
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
window.iterations = 100000;
Tests:
OOP
class Obj { constructor(x, y, z) { this.x = x; this.y = y; this.z = z; this.result = x + y + z; } update(x, y, z) { this.x = x; this.y = y; this.z = z; this.result = x + y + z; } } const OOP = []; for (let i = 0; i < iterations; i++ ) { OOP.push(new Obj(Math.random(), Math.random(), Math.random())); }; for (let i = 0; i < iterations; i++ ) { OOP[i].update(Math.random(), Math.random(), Math.random()); };
FP
function createObj(x, y, z) { return { x, y, z, result: x + y + z } } function updateObj(obj, x, y, z) { obj.x = x; obj.y = y; obj.z = z; obj.result = x + y + z; } const FP = []; for (let i = 0; i < iterations; i++ ) { FP.push(createObj(Math.random(), Math.random(), Math.random())); }; for (let i = 0; i < iterations; i++ ) { updateObj(FP[i], Math.random(), Math.random(), Math.random()); };
Fake OOP
function createObj(x, y, z) { return { x, y, z, result: x + y + z } } function updateObj(obj, x, y, z) { obj.x = x; obj.y = y; obj.z = z; obj.result = x + y + z; } const FakeOOP = []; for (let i = 0; i < iterations; i++ ) { const obj = createObj(Math.random(), Math.random(), Math.random()); obj.update = (x, y, z) => updateObj(obj, x, y, z); FakeOOP.push(obj); }; for (let i = 0; i < iterations; i++ ) { FakeOOP[i].update(Math.random(), Math.random(), Math.random()); };
Fake OOP2
function createObj(x, y, z) { return { x, y, z, result: x + y + z } } function updateObj(obj, x, y, z) { obj.x = x; obj.y = y; obj.z = z; obj.result = x + y + z; } class Obj2 { constructor(x, y, z) { this.obj = createObj(x, y, z); } update(x, y, z) { updateObj(this.obj, x, y, z); } } const FakeOOP2 = []; for (let i = 0; i < iterations; i++ ) { FakeOOP2.push(new Obj2(Math.random(), Math.random(), Math.random())); }; for (let i = 0; i < iterations; i++ ) { FakeOOP2[i].update(Math.random(), Math.random(), Math.random()); };
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
OOP
FP
Fake OOP
Fake OOP2
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
gemma2:9b
, generated one year ago):
The benchmark results show that the "FP" test is the fastest, executing at 10.3057 executions per second. Here's a breakdown: * **FP:** Fastest, likely due to its simplicity and direct use of built-in functions. * **Fake OOP2:** Second fastest, demonstrating that object-oriented structures with clear update methods can be efficient. * **OOP:** Slower than "Fake OOP2," suggesting that the additional complexity of class definitions might introduce some overhead. * **Fake OOP:** The slowest, potentially because it relies on mutable objects and function calls within a loop, adding more steps compared to other tests. **Important Considerations:** * **Small Sample Size:** These results are based on a single run. More iterations would provide a more reliable average. * **JavaScript Engine:** The specific JavaScript engine (V8 in Chrome in this case) significantly influences performance. * **Hardware:** The system's CPU and memory also play a role. **Overall:** While "FP" is the clear winner in this particular benchmark, real-world applications often benefit from the structure and organization provided by object-oriented programming. Choosing the best approach depends on the specific problem and its complexity.
Related benchmarks:
Array creation with Array.reduce vs for loop vs Array.forEach
Queue Implement by 2 Stacks
Array.reduce vs for loop vs Array.forEachasdfasdfas
Spread Splice vs Spread Push (with object IDs)
Closure overhead vs function overhead vs inline
Comments
Confirm delete:
Do you really want to delete benchmark?