Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
percentChange
(version: 5)
Percent Change values formatter
Comparing performance of:
Numeral js vs Regex vs Math.floor vs parseFloat
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/accounting.js/0.1/accounting.min.js"></script>
Tests:
Numeral js
let a = 121230.4252; numeral(a).format('0,0.00')+'%';
Regex
let a = 121230.4252; a.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")+"%"
Math.floor
let a = 121230.4252; (~~(a * 100) / 100).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")+"%";
parseFloat
let a =121230.4252; Number.parseFloat(a).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")+"%";
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Numeral js
Regex
Math.floor
parseFloat
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.1:latest
, generated one year ago):
Let's dive into the details of this benchmark. **Benchmark Description** The benchmark is called "percentChange" and its purpose is to measure the performance of different approaches to format a number with two decimal places and append a percent sign (%) to it. **Test Cases** There are four test cases: 1. **Numeral js**: This test uses the `numeral.js` library, which is a popular JavaScript library for formatting numbers. 2. **Regex**: This test uses regular expressions (regex) to format the number. 3. **Math.floor**: This test uses the `Math.floor()` function to perform arithmetic operations and then formats the result using regex. 4. **parseFloat**: This test uses the `Number.parseFloat()` function to parse a floating-point number and then formats it using regex. **Benchmark Results** The latest benchmark results show that: * On Chrome 96 (Desktop, Windows), Math.floor performs best with approximately 2.1 million executions per second. * Regex is the next fastest with around 1.3 million executions per second. * parseFloat comes third with about 858 thousand executions per second. * Numeral js is the slowest with roughly 156 thousand executions per second. **Library and Features Used** The `numeral.js` library is used in test case 1 (Numeral js). This library provides a simple way to format numbers with different patterns, including decimal places. In test cases 3 (Math.floor) and 4 (parseFloat), the `Math.floor()` function and `Number.parseFloat()` function are used respectively. These functions perform basic arithmetic operations. Test case 2 (Regex) uses regular expressions to format the number. The regex pattern `/\\B(?=(\\d{3})+(?!\\d))/g` is used to insert commas as thousand separators. **Alternatives** Other alternatives to achieve this formatting could be: * Using the built-in `toLocaleString()` method, which can format numbers with decimal places and append a currency symbol. * Implementing a custom function using bitwise operations or mathematical calculations. Keep in mind that these alternatives might have different performance characteristics compared to the ones tested here.
Related benchmarks:
lodash.round VS toFixed() VS toFixed() and parseFloat vs L.formmatNum
decimal.js vs. native
number ass testing v2
Native.toFixed() vs bignumber.js vs big.js vs decimal.js
Comments
Confirm delete:
Do you really want to delete benchmark?