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
llama3.1:latest
, generated one year ago):
**Benchmark Overview** This benchmark compares the performance of three different approaches for creating and updating objects in JavaScript: 1. **Functional Programming (FP)**: This approach uses functions to create and update objects, without modifying the original object. 2. **Object-Oriented Programming (OOP)**: This approach uses classes and instances to create and update objects, with each instance maintaining its own state. 3. **Fake Object-Oriented Programming (Fake OOP)**: This approach mimics OOP by creating an object-like structure using functions, but without the overhead of class instances. **Test Cases** There are four test cases: 1. **FP**: Creates an array of objects using a function `createObj` and updates them using another function `updateObj`. The objects are not modified in place. 2. **OOP**: Creates an array of class instances `Obj` using the `new` keyword, and updates them by calling the `update` method on each instance. 3. **Fake OOP 1**: Creates an array of objects using a function `createObj`, but adds a `update` property to each object that calls the `updateObj` function. This mimics OOP-like behavior without creating class instances. 4. **Fake OOP 2**: Similar to Fake OOP 1, but uses a separate class `Obj2` with a `constructor` and an `update` method that calls `updateObj`. The objects are not modified in place. **Benchmark Results** The latest benchmark results show the execution speed (Executions Per Second) for each test case on a desktop Chrome browser: * FP: 10.31 EPS * Fake OOP2: 9.09 EPS * OOP: 8.67 EPS * Fake OOP1: 8.56 EPS **Conclusion** The results suggest that Functional Programming (FP) is the fastest approach, followed by Fake Object-Oriented Programming (Fake OOP), and then traditional Object-Oriented Programming (OOP). However, it's essential to note that these results are specific to this particular benchmark and might not generalize to other use cases or scenarios. Keep in mind that the performance differences between these approaches can be influenced by various factors, such as the complexity of the objects being created and updated, the frequency of updates, and the specific browser or environment being used.
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?