Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Functional Vs Imperative 10 elems
(version: 0)
Comparing performance of:
Functional vs Imperative
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.nums = new Array(10).fill(0).map((x,i) => i)
Tests:
Functional
const y = nums.filter(x => x%2).map(x => x*3).filter(x => x < 100) return y
Imperative
const y = [] for(const x of nums) { if(x%2) { const x3 = x*3; if(x3 < 100) { y.push(x3) } } } return y
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Functional
Imperative
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Chrome OS 14541.0.0
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Functional
2092581.2 Ops/sec
Imperative
10626187.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks to compare the performance of different programming approaches. **Benchmark Definition JSON Analysis** The provided JSON represents the benchmark definition, which includes: * `Name`: The name of the benchmark, "Functional Vs Imperative 10 elems". * `Description`: An empty description for this benchmark. * `Script Preparation Code`: A JavaScript code that initializes an array `nums` with 10 elements, each with a unique index value. This is used to filter and manipulate the data in both test cases. * `Html Preparation Code`: An empty string indicating no HTML preparation is required. **Individual Test Cases** There are two individual test cases: 1. **Functional Test Case** The code uses the `filter` and `map` methods to process the `nums` array: ```javascript const y = nums.filter(x => x%2).map(x => x*3).filter(x => x < 100) return y ``` This approach is considered functional programming, where a function takes an input (the array) and returns a new output without modifying the original. **Pros of Functional Approach:** * Easier to write and maintain code, as it's more focused on data transformation. * Less prone to side effects and mutable state issues. * Can be more composable and reusable. **Cons of Functional Approach:** * May require additional memory allocation for creating new arrays, potentially leading to performance overhead. * May have slower execution due to the creation of temporary intermediate results. 2. **Imperative Test Case** The code uses a `for` loop and array push to process the `nums` array: ```javascript const y = [] for (const x of nums) { if(x%2) { const x3 = x*3; if(x3 < 100) { y.push(x3) } } } return y ``` This approach is considered imperative programming, where a function explicitly controls the execution flow and modifies state. **Pros of Imperative Approach:** * Can be more efficient in terms of memory allocation, as it avoids creating new arrays. * May have faster execution due to the direct manipulation of the array. **Cons of Imperative Approach:** * Can lead to more complex code and harder-to-debug issues due to explicit state management. * May result in performance overhead due to repeated array lookups and updates. **Library and Special JS Feature Mention** There is no library mentioned in the provided benchmark definition or test cases. No special JavaScript features are used beyond standard language features like `filter`, `map`, `for` loops, and conditional statements. **Other Alternatives** Some other programming approaches that could be compared on MeasureThat.net include: * Declarative programming (e.g., Prolog) * Object-oriented programming (OOP) with encapsulation and inheritance * Event-driven programming * Functional reactive programming (FRP) Note that each approach has its own trade-offs in terms of code complexity, performance, and maintainability. MeasureThat.net provides a platform for users to experiment and compare different programming styles, allowing them to find the most suitable approach for their specific needs.
Related benchmarks:
isArray vs instanceof vs Symbol.iterator vs Object.prototype.toString.call
Map vs switch soh
lodash map vs es6 Object.values.map
Functional Vs Imperative
Comments
Confirm delete:
Do you really want to delete benchmark?