Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
higher order function vs simple function perf check
(version: 2)
higher order function vs simple function perf check
Comparing performance of:
simple function vs higher order function
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var artifacts = { getById: () => [] }
Tests:
simple function
const flatten = (artifacts, artifactId) => { const getDependencies = (artifactId) => artifacts.getById(artifactId); return [ ...getDependencies(artifactId) ] } for(var i = 0; i < 100; i++) { flatten(artifacts, i) }
higher order function
const createFlattenr = (artifacts) => { const getDependencies = (artifactId) => artifacts.getById(artifactId); return (artifactId) => { return [ ...getDependencies(artifactId) ] } } const flatten = createFlattenr(artifacts) for(var i = 0; i < 100; i++) { flatten(i) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
simple function
higher order function
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
simple function
1522201.8 Ops/sec
higher order function
1522021.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **Benchmark Overview** The benchmark measures the performance difference between two approaches: using a simple function and using a higher-order function (a function that takes another function as an argument). The test case generates an array of dependencies by calling a `getDependencies` function, which is either directly called or passed as an argument to the main `flatten` function. **Options Compared** Two options are compared: 1. **Simple Function**: A straightforward function that calls `getDependencies` directly. 2. **Higher-Order Function (HOF)**: A function that takes another function (`createFlattenr`) as an argument and returns a new function (`flatten`). The HOF is then called with the `artifactId` parameter. **Pros and Cons** **Simple Function:** Pros: * Easy to understand and implement * Less memory overhead since it doesn't store the outer function Cons: * May incur additional overhead due to indirect function calls * Can be slower due to the extra layer of indirection **Higher-Order Function (HOF):** Pros: * Can lead to better performance by avoiding unnecessary indirection * Allows for more flexibility in function composition and reuse Cons: * More complex to understand and implement, especially for those new to HOFs * May incur additional memory overhead due to storing the outer function **Library:** The `artifacts` object is a custom library that provides a simple way to retrieve an array of dependencies by calling `getDependencies(artifactId)`. This library is not part of the JavaScript standard library and is specific to this benchmark. **Special JS Feature/Syntax:** There is no special JavaScript feature or syntax used in this benchmark. The code follows standard JavaScript syntax and conventions. **Other Alternatives:** If you were to rewrite this benchmark, other alternatives could include: 1. Using a more modern language like TypeScript or C# with its function composition features. 2. Employing compiler optimizations or transpilation techniques to minimize the performance gap between simple and HOF functions. 3. Considering alternative data structures or algorithms that might provide better performance for specific use cases. Keep in mind that the performance difference between simple and higher-order functions can be significant, especially for large datasets. The benchmark's results demonstrate this: the HOF approach is approximately 4.5x faster than the simple function approach on a desktop machine running Chrome 110.
Related benchmarks:
Var vs Let
+string vs Number vs parseInt
Implicit vs parseInt vs Number string to num
Number vs + vs parseFloat + properties px
typeof number vs. Number.isNan vs. isNan vs self comparison. Versus let
Comments
Confirm delete:
Do you really want to delete benchmark?