Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
curryChain
(version: 0)
Comparing performance of:
Raw vs Direct vs Chain
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
window.round = (value, decimalScale) => { const scale = Math.pow(10, decimalScale); return Math.round(value * scale) / scale; } window.snap = (value, increment) => { return Math.round(value / increment) * increment; } window.clamp = (value, min, max) => { return Math.min(Math.max(value, min), max); } const noop = () => {}; const curry = (fn, right) => (...args) => right ? v => fn(...args, v) : v => fn(v, ...args); const chain = fn => (...args) => { const result = fn(...args); return new Proxy(noop, { apply: (_, __, [value]) => result(value), get: (_, prop) => chain((...nextArgs) => { const nextResult = utils[prop](...nextArgs); return value => nextResult(result(value)); }) }); } const makeChainable = (fn, right = 0) => (...args) => (args.length < fn.length ? chain(curry(fn, right)) : fn)(...args); window.utils = { round: makeChainable(round), snap: makeChainable(snap), clamp: makeChainable(clamp), }; const randomNumber = Math.random(); window.cachedRaw = v => round(clamp(snap(v, 10.2222), 20.5555, 40.5555), 2); window.cachedDirect = v => utils.round(utils.clamp(utils.snap(v, 10.2222), 20.5555, 40.5555), 2); window.cachedChain = utils.snap(10.2222).clamp(20.5555, 40.5555).round(2);
Tests:
Raw
cachedRaw(Math.random() * 100);
Direct
cachedDirect(Math.random() * 100);
Chain
cachedChain(Math.random() * 100);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Raw
Direct
Chain
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):
Let's break down the provided JSON and explain what is tested on the benchmark. **Benchmark Definition** The benchmark definition provides a JavaScript function that defines several utility functions: `round`, `snap`, and `clamp`. These functions are used to manipulate numbers. The `curry` and `chain` functions are used to create chainable versions of these utilities. * `round`: rounds a number to a specified decimal scale. * `snap`: returns the nearest multiple of an increment value. * `clamp`: limits a value within a specified range. **Options Compared** The benchmark compares three different approaches to perform the same calculation: 1. **Raw**: The original implementation that calls `cachedRaw` directly. 2. **Direct**: An implementation that uses `cachedDirect` to calculate the result. 3. **Chain**: An implementation that uses `cachedChain` to calculate the result. **Pros and Cons of Each Approach** * **Raw**: + Pros: Simple, straightforward implementation. + Cons: May not be optimized for performance. * **Direct**: + Pros: Can potentially be faster than Raw since it avoids the overhead of `utils`. + Cons: Requires more function calls, which can increase overhead. * **Chain**: + Pros: Can potentially be faster and more efficient due to lazy evaluation. + Cons: May require more memory allocation and deallocation. **Library** The benchmark uses a custom library called `utils` that provides the `round`, `snap`, and `clamp` functions. This library is created using the `makeChainable` function, which creates chainable versions of these utilities. * `utils`: A custom library that provides utility functions for rounding, snapping, and clamping numbers. * `makeChainable`: A function that takes a utility function and returns a chainable version of it. **Special JS Feature** The benchmark uses the `Proxy` object to create a proxy around the `noop` function. This is not a special JavaScript feature per se, but rather a common technique for implementing lazy evaluation. * `Proxy`: A built-in JavaScript object that allows creating proxies around other objects or functions. Now, let's discuss the test cases and benchmark results: The benchmark consists of three test cases: Raw, Direct, and Chain. Each test case calls one of the utility functions (`cachedRaw`, `cachedDirect`, or `cachedChain`) with a random number. The benchmark results show that the Chain approach is the fastest, followed by Direct, and then Raw. This suggests that lazy evaluation using the `chain` function can lead to significant performance improvements in this specific use case.
Related benchmarks:
roundDecimal: pow() vs. toPrecision()
Round expo vs round normal
Math.round vs bitRound
Math.round vs bitRound v2
Comments
Confirm delete:
Do you really want to delete benchmark?