Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
i hate l00ps with optimaze for
(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,len =numbers.length; i <len ; 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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 YaBrowser/24.1.0.0 Safari/537.36
Browser/OS:
Yandex Browser 24 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
functional
1094.7 Ops/sec
imperative
901.2 Ops/sec
functional but better
1152.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared, and other considerations. **Benchmark Overview** The provided JSON represents a set of benchmarks that test the performance of different JavaScript implementations ( likely browsers) in executing mathematical operations on an array of random numbers. The benchmarks are categorized into three types: 1. "functional" - uses the `map()`, `filter()`, and `reduce()` methods to perform the operation. 2. "imperative" - uses a traditional loop-based approach with indexing to perform the operation. 3. "functional but better" - an optimized version of the "functional" benchmark, which uses a different implementation for the reduction step. **Tested Operations** The benchmarks test the performance of three operations: 1. Mapping and filtering the array of numbers to select only even numbers (after rounding). 2. Performing arithmetic operations on individual elements of the array. 3. Reducing the filtered array to sum up the values (using different methods). **Comparison of Approaches** Here's a brief overview of each approach, their pros, and cons: 1. **Functional Approach ("functional" and "functional but better")** * Pros: + Declarative programming style makes it easier to understand and maintain. + Reduces the risk of off-by-one errors or other arithmetic issues. + Can be optimized more effectively using algorithms and data structures. * Cons: + May have higher overhead due to function calls and object lookups. + Can lead to slower execution times for large datasets. 2. **Imperative Approach ("imperative")** * Pros: + Often faster due to reduced function call overhead. + Can be more suitable for large, complex datasets. * Cons: + More error-prone and prone to off-by-one errors or arithmetic issues. + Requires manual indexing and management of loop variables. **Library Usage** There is no explicit library usage mentioned in the benchmark definitions. However, some browsers might have built-in optimizations or features that could affect performance, such as: * `Map` and `Set` data structures (used by modern browsers) * SIMD instructions (used by some browsers like Chrome) **Specialized JavaScript Features** The benchmark uses standard JavaScript syntax and does not include any specialized features like: * `async/await` * Generators * Proxies No other alternative implementations or optimizations are provided in the benchmark results. **Alternatives** For testing performance differences, alternatives to MeasureThat.net could be: * Google Benchmark (for C++ benchmarks) * PyBench (for Python benchmarks) * JMH (for Java benchmarks) These tools allow users to define and run benchmarks for their specific use cases, using a variety of programming languages.
Related benchmarks:
Fill array with random integers
.at vs [x]
at 500 vs [500]
Math.random vs Crypto.getRandomValues for 10k values
new Array() vs Array.from() with random data
Comments
Confirm delete:
Do you really want to delete benchmark?