Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Negative precision floor: Lodash vs Math.floor
(version: 0)
Comparing performance of:
_.max vs Math.max
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.7.11/lodash.min.js'></script>
Script Preparation code:
function getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); } var arr = []; for(var i = 0; i < 1000; i++){ arr.push({value:getRandomInt(100)}); }
Tests:
_.max
for (let i = 0; i < arr.length; i++) _.floor(arr[i], -2);
Math.max
for (let i = 0; i < arr.length; i++) Math.floor(arr[i] * 100) / 100;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.max
Math.max
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.max
1679.7 Ops/sec
Math.max
5544.5 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 Definition JSON** The provided JSON defines two benchmarks: 1. **Negative precision floor: Lodash vs Math.floor** * This benchmark tests the performance of `_.floor` (from Lodash) versus `Math.floor` for rounding negative numbers to the nearest integer. 2. **_.max vs Math.max** * This benchmark compares the performance of `_.max` (from Lodash) versus `Math.max` for finding the maximum value in an array. **Script Preparation Code** The script preparation code generates an array of 1000 objects, each containing a random integer value between -100 and 100. This array will be used as input for the benchmark tests. **Html Preparation Code** The HTML preparation code includes a reference to the Lodash library (version 4.7.11), which provides the `_.floor` and `_.max` functions being tested in the benchmarks. **Benchmark Test Cases** There are two test cases: 1. **_.max** * The benchmark definition is: `for (let i = 0; i < arr.length; i++) _.floor(arr[i], -2);` * This test case uses the Lodash library's `_.max` function to find the maximum value in the array. 2. **Math.max** * The benchmark definition is: `for (let i = 0; i < arr.length; i++) Math.floor(arr[i] * 100) / 100;` * This test case uses the built-in JavaScript `Math.max` function to find the maximum value in the array. **Pros and Cons** Here are some pros and cons of each approach: 1. **Lodash's _.max** * Pros: + More concise and expressive code + Can handle more complex data structures (e.g., arrays with objects) * Cons: + May have a higher overhead due to the additional library dependencies 2. **Built-in JavaScript Math.max** * Pros: + Faster execution due to native implementation + No external dependencies required * Cons: + Less expressive code (may require more intermediate steps) + Limited support for complex data structures **Other Considerations** When choosing between these two approaches, consider the trade-off between performance and code readability. If performance is critical and the input data is simple (e.g., an array of integers), using `Math.max` might be a better choice. However, if the input data is more complex or you need to perform additional operations on the maximum value, Lodash's `_.max` might be a better option. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, string manipulation, and functional programming. The `_.floor` and `_.max` functions are part of the Lodash library, which provides a concise and expressive way to perform common data processing tasks. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax used in this benchmark. However, if you're interested in exploring more advanced topics, some other features worth mentioning include: * ES6's `Promise` API for handling asynchronous operations * `async/await` syntax for simplifying promise-based code * WebAssembly (WASM) for performance-critical applications As for alternatives to Lodash or the built-in JavaScript `Math.max`, some popular options include: * **Ramda**: A functional programming library that provides a different set of utility functions. * **Underscore.js**: Another popular JavaScript utility library that offers many of the same features as Lodash. * **VanillaJS**: Using native JavaScript functions and optimizations for performance-critical applications.
Related benchmarks:
Lodash max vs Math.max (lodash 4.7.11)
Lodash min vs Math.min (lodash 4.7.11)
Negative precision floor: Lodash vs Math.floor #2
Lodash max vs JS Math.max (2022)
Comments
Confirm delete:
Do you really want to delete benchmark?