Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
i hate l00ps
(version: 0)
Comparing performance of:
functional vs imperative vs functional but better
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var numbers = Array.from({ length: 10_000 }).map(() => Math.random())
Tests:
functional
const result = numbers .map(n => Math.round(n * 10)) .filter(n => n % 2 === 0) .reduce((a, n) => a + n, 0)
imperative
let result = 0 for (let i = 0; i < numbers.length; i++) { let n = Math.round(numbers[i] * 10) if (n % 2 !== 0) continue result = result + n }
functional but better
const round = n => Math.round(n * 10) const isEven = n => n % 2 === 0 const result = numbers .reduce((a, n) => { n = round(n) return isEven(n) ? a + n : a; }, 0)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
functional
imperative
functional but better
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 123 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
functional
622.1 Ops/sec
imperative
445.0 Ops/sec
functional but better
664.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The provided JSON represents a benchmark with three test cases: "functional", "imperative", and "functional but better". The goal is to compare the execution performance of different approaches for performing a specific calculation on an array of random numbers. **Test Case 1: "functional"** This test case uses the `.map()`, `.filter()`, and `.reduce()` methods to calculate the sum of even numbers in the array. The code is written in a functional programming style, which is declarative and focuses on describing what needs to be done rather than how it's done. **Test Case 2: "imperative"** This test case uses a traditional loop-based approach to iterate over the array and calculate the sum of even numbers. This style is more imperative, as it focuses on the step-by-step actions needed to achieve the result. **Test Case 3: "functional but better"** This test case takes inspiration from the functional style while introducing some performance optimizations. It uses a `round` function and an `isEven` predicate to simplify the calculation and make it more readable. **Comparison of Approaches** The three approaches differ in their syntax, readability, and performance characteristics: * **Functional**: This approach is concise and declarative, but it may incur overhead due to method call lookups and unnecessary object creations. Performance-wise, it's often slower than imperative methods. * **Imperative**: This approach is straightforward and easy to understand, but it can be verbose and prone to errors. It tends to perform better than functional approaches since it avoids the overhead of method calls and unnecessary data copies. * **Functional but better**: This approach balances readability with performance by introducing optimizations like early returns and predicate functions. While still not as fast as imperative methods, it strikes a good balance between conciseness and efficiency. **Library Usage** In this benchmark, no libraries are explicitly mentioned or required. The code is self-contained and focuses on demonstrating the differences between functional and imperative programming styles in JavaScript. **Special JS Features** The test cases don't explicitly use any special features like async/await, generators, or modern ES6+ features (except for template literals). However, some minor details like `let` vs. `var` and `const` declarations are used to demonstrate their differences in a practical context. **Other Alternatives** For similar benchmarks, you can explore other JavaScript microbenchmarking frameworks like: * V8 Benchmark * Node.js Microbenchmarking * jsPerf These tools provide more features, flexibility, and customization options for creating and running microbenchmarks. Feel free to ask if you have any further questions!
Related benchmarks:
Fill array with random integers
Labels
.at vs [x]
Math.random vs Crypto.getRandomValues for 10k values
parse float
Comments
Confirm delete:
Do you really want to delete benchmark?