Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Clamp Math vs Lodash clamp
(version: 0)
Testing math min, clamping vs lodash clamping
Comparing performance of:
R Clamp vs Lodash clamp vs JS Clamp
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
Script Preparation code:
var data = _.range(10000).map(function(i) { return { counter: Math.floor(Math.random() * 10) } }); function clamp(value, min, max) { return Math.max(min, Math.min(value, max)); }
Tests:
R Clamp
var result = data.map((d) => R.clamp(1, 5, d.counter));
Lodash clamp
var result = data.map((d) => _.clamp(1, 5, d.counter));
JS Clamp
var result = data.map((d) => clamp(1, 5, d.counter));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
R Clamp
Lodash clamp
JS Clamp
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:145.0) Gecko/20100101 Firefox/145.0
Browser/OS:
Firefox 145 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
R Clamp
8524.0 Ops/sec
Lodash clamp
6202.3 Ops/sec
JS Clamp
22803.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark measures the performance of three different implementations of a clamp function: 1. `R.Clamp` (using Ramda library): Clamps a value to a specified minimum and maximum. 2. `Lodash clamp`: Clamps a value to a specified minimum and maximum using Lodash library. 3. `JS Clamp` (custom implementation): Clamps a value to a specified minimum and maximum using only JavaScript. **Options Compared** The three implementations are compared in terms of performance, specifically the number of executions per second. * **Ramda (`R.Clamp`)**: Uses Ramda's `clamp` function from its `R` namespace. Ramda is a functional programming library for JavaScript. * **Lodash (`Lodash clamp`)**: Uses Lodash's `clamp` function from its `_` namespace. Lodash is a utility library that provides functions for tasks such as array manipulation, string templating, and more. * **JS Clamp**: A custom implementation of the clamp function in pure JavaScript. **Pros and Cons** Here are some pros and cons of each approach: * **Ramda (`R.Clamp`)**: + Pros: Lightweight and efficient due to its functional programming nature. Ramda's `clamp` function is specifically optimized for performance. + Cons: Requires including an additional library (Ramda) in the benchmark. * **Lodash (`Lodash clamp`)**: + Pros: Wide adoption and a large community of developers who have tested and optimized this implementation. + Cons: Slightly heavier than Ramda due to its utility library nature. Lodash's `clamp` function may have some overhead from its general-purpose functionality. * **JS Clamp**: + Pros: Only uses JavaScript, making it easy to implement and test locally. + Cons: May not be as optimized or efficient as the other two implementations. **Library Descriptions** * Ramda is a functional programming library that provides a set of reusable functions for tasks such as array manipulation, string templating, and more. Its `clamp` function takes three arguments (min, max, and value) and returns the clamped value. * Lodash is a utility library that provides a wide range of functions for tasks such as array manipulation, string templating, and more. Its `clamp` function does exactly what its name suggests: clamps a value to a specified minimum and maximum. **Special JS Feature/Syntax** This benchmark does not use any special JavaScript features or syntax beyond the standard ECMAScript 2022 specification.
Related benchmarks:
Lodash clamp vs Math.min(Math.max)
flatten : Lodash vs Ramda
Lodash clamp VS Math.min, Math.max (Conditional)
test lodash clamp vs math.min
Comments
Confirm delete:
Do you really want to delete benchmark?