Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
FP vs OOP vs fake OOP 7
(version: 3)
Comparing performance of:
OOP vs Fake OOP2 vs Fake OOP3 vs FP
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.update(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()); };
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()); };
Fake OOP3
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 Obj3 { constructor(x, y, z) { Object.assign(this, createObj(x, y, z)); } update(x, y, z) { updateObj(this, x, y, z); } } const FakeOOP3 = []; for (let i = 0; i < iterations; i++ ) { FakeOOP3.push(new Obj3(Math.random(), Math.random(), Math.random())); }; for (let i = 0; i < iterations; i++ ) { FakeOOP3[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()); };
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
OOP
Fake OOP2
Fake OOP3
FP
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 different programming paradigms can be a fascinating exercise. The provided JSON benchmark test case is designed to compare the performance of three approaches: Object-Oriented Programming (OOP), Functional Programming (FP), and a "fake" OOP approach, which we'll discuss later. **Object-Oriented Programming (OOP)** In this approach, a class `Obj` is defined with an `update` method that modifies the object's properties. The test creates an array of objects using the `new` keyword, pushes them to the `OOP` array, and then updates each object in the array. Pros: * Easy to understand and implement * Well-established paradigm with many libraries and tools available Cons: * Can be slower due to the overhead of method calls and property accesses * May lead to tight coupling between objects **Functional Programming (FP)** In this approach, a `createObj` function is used to create objects, which are then pushed to an array. The `updateObj` function is applied to each object in the array. Pros: * Can be faster due to the avoidance of method calls and property accesses * Encourages immutability, making it easier to reason about code Cons: * May require more boilerplate code (e.g., creating a new object for each update) * Can lead to less modular code if not designed carefully **Fake OOP Approach** The "fake" OOP approach uses arrays and functions to mimic the behavior of classes. In this case, an array `FP` is created using the `createObj` function, which returns an object with properties. The `updateObj` function is then applied to each element in the array. Pros: * Can be similar performance to OOP due to the avoidance of method calls and property accesses * May require less boilerplate code than FP Cons: * Does not provide the same level of encapsulation as traditional OOP * May lead to confusion if not clearly explained or documented **Benchmark Results** The latest benchmark results show that: 1. FP is the fastest approach, with an average execution rate of 2.513136863708496 executions per second. 2. Fake OOP is the next fastest, with an average execution rate of 1.6504676342010498 executions per second. 3. OOP is the slowest, with an average execution rate of 1.5134466461995014 executions per second. Overall, the results suggest that FP and fake OOP approaches can be faster than traditional OOP due to their avoidance of method calls and property accesses. However, it's essential to consider the trade-offs between performance and maintainability when choosing an approach.
Related benchmarks:
Array creation with Array.reduce vs for loop vs Array.forEach
Array creation with Array.reduce vs for loop vs Array.forEach(2)
Array.reduce vs for loop vs Array.forEachasdfasdfas
Math.floor(Math.random() * 1000000000).toString() vs window.performance.now().toFixed()
toFixed vs toPrecision vs Math.round() feat. Math.pow
Comments
Confirm delete:
Do you really want to delete benchmark?