Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
tastastasta
(version: 0)
Comparing performance of:
number vs lodash
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var intA = 42.034; var strB = "42.034";
Tests:
number
var res = Number(intA) + Number(strB);
lodash
var reTrim = /^\s+|\s+$/g; var reIsBinary = /^0b[01]+$/i; var reIsBadHex = /^[-+]0x[0-9a-f]+$/i; var reIsOctal = /^0o[0-7]+$/i; function isSymbol(value) { return typeof value == 'symbol' || (isObjectLike(value) && baseGetTag(value) == symbolTag); } function isObjectLike(value) { return value != null && typeof value == 'object'; } function isObject(value) { var type = typeof value; return value != null && (type == 'object' || type == 'function'); } function toNumber(value) { if (typeof value == 'number') { return value; } if (isSymbol(value)) { return NAN; } if (isObject(value)) { var other = typeof value.valueOf == 'function' ? value.valueOf() : value; value = isObject(other) ? (other + '') : other; } if (typeof value != 'string') { return value === 0 ? value : +value; } value = value.replace(reTrim, ''); var isBinary = reIsBinary.test(value); return (isBinary || reIsOctal.test(value)) ? freeParseInt(value.slice(2), isBinary ? 2 : 8) : (reIsBadHex.test(value) ? NAN : +value); } var res = toNumber(intA) + toNumber(strB);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
number
lodash
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 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
number
4769958.5 Ops/sec
lodash
5394613.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **What is being tested?** The provided benchmark tests two different approaches: 1. `number`: This test case measures the performance of a simple addition operation using the built-in `Number()` function in JavaScript. The input values are stored as decimal numbers (`intA = 42.034` and `strB = "42.034"`). 2. `lodash`: This test case measures the performance of a more complex expression that uses the Lodash library's `toNumber()` function to convert non-numeric inputs (strings, symbols, objects) to numbers. **Options compared** The two tests compare the following options: 1. Built-in JavaScript `Number()` function vs. custom Lodash `toNumber()` function. 2. Simple addition operation (`var res = Number(intA) + Number(strB);`) vs. more complex expression with multiple checks and conversions (`var res = toNumber(intA) + toNumber(strB);`). **Pros and Cons** Here are the pros and cons of each approach: 1. Built-in JavaScript `Number()` function: * Pros: Simple, easy to understand, and widely supported. * Cons: May not handle all edge cases or unexpected input types well. 2. Custom Lodash `toNumber()` function: * Pros: More comprehensive handling of non-numeric inputs (strings, symbols, objects), but more complex implementation. * Cons: Requires the Lodash library to be loaded and may add overhead. **Library usage** The test case uses the Lodash library for its `toNumber()` function. The purpose of this function is to convert non-numeric values to numbers, handling various cases such as: * Strings (with trimming and parsing) * Symbols * Objects (using their `valueOf` method or string conversion) **Special JS feature or syntax** None mentioned in the provided benchmark definition. **Other alternatives** If you wanted to rewrite the tests without using the Lodash library, you could implement a custom solution for converting non-numeric inputs to numbers. Some approaches might include: * Using regular expressions to detect and handle different input types * Implementing a more comprehensive set of checks and conversions using JavaScript functions like `typeof`, `NaN`, and `Number` * Utilizing other libraries or frameworks that provide similar functionality (e.g., Moment.js for date parsing) Keep in mind that rewriting the tests without the Lodash library would likely result in a more complex implementation, potentially affecting performance.
Related benchmarks:
parseInt vs Number addition
parseInt vs Number vs Plus addition
parseInt vs Number addition Fork
parseInt vs Number vs ~~
parseInt vs Number mult
Comments
Confirm delete:
Do you really want to delete benchmark?