Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Format number | Regex vs Code V1.1
(version: 1)
Regex VS Code for number format
Comparing performance of:
regex test (toString) vs code test (toString) vs regex test ```` vs code test ````
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var char = ' ' var count = 3 function formatNumber(num) { const arr = num.toString().split(''); let it = 1; for (let i = arr.length - 1; i >= 1; i--) { if (it % count === 0) { arr.splice(i, 0, char); } it++; } return arr.join(''); } function formatNumber1bis(num) { const arr = `${num}`.split(''); let it = 1; for (let i = arr.length - 1; i >= 1; i--) { if (it % count === 0) { arr.splice(i, 0, char); } it++; } return arr.join(''); } function formatNumber2(num) { return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, char); } function formatNumber2bis(num) { return `${num}`.replace(/\B(?=(\d{3})+(?!\d))/g, char); }
Tests:
regex test (toString)
const numbers = [1, 100, 1000, 10000, 100000, 1000000, 10000000, 1000000000] for (let i = 0; i < numbers.length; i++) { formatNumber2(numbers[i]) }
code test (toString)
const numbers = [1, 100, 1000, 10000, 100000, 1000000, 10000000, 1000000000] for (let i = 0; i < numbers.length; i++) { formatNumber(numbers[i]) }
regex test ````
const numbers = [1, 100, 1000, 10000, 100000, 1000000, 10000000, 1000000000] for (let i = 0; i < numbers.length; i++) { formatNumber2bis(numbers[i]) }
code test ````
const numbers = [1, 100, 1000, 10000, 100000, 1000000, 10000000, 1000000000] for (let i = 0; i < numbers.length; i++) { formatNumber1bis(numbers[i]) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
regex test (toString)
code test (toString)
regex test ````
code test ````
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 dive into the world of JavaScript microbenchmarks! **Benchmark Definition JSON** The provided JSON represents a benchmark definition for a JavaScript function called `formatNumber`. The benchmark compares three different approaches to format numbers: 1. **Regex approach**: Uses regular expressions (regex) to manipulate the number string. 2. **Code V1.1 approach**: Implements a custom implementation using traditional JavaScript coding style. **Options Compared** The benchmark compares the performance of two regex-based approaches and two code-based approaches. The options are: * `formatNumber2` (Regex approach): uses a regex pattern to replace whitespace with the specified character. * `formatNumber2bis` (Regex approach): uses a similar regex pattern, but applied to a string template literal (`${num}`) instead of a plain string. * `formatNumber1bis` (Code V1.1 approach): implements a custom implementation using traditional JavaScript coding style. **Pros and Cons** Here are some pros and cons for each approach: * **Regex approach**: + Pros: concise, flexible, and easy to read. + Cons: can be slow due to regex engine overhead and potential backtracking issues. * **Code V1.1 approach**: + Pros: simple, readable, and potentially faster since it avoids regex engine overhead. + Cons: may not be as concise or flexible as the regex approach. **Library Usage** The `String.prototype.replace()` method is used in both regex-based approaches (`formatNumber2` and `formatNumber2bis`). This method replaces occurrences of a pattern with a replacement string. In this case, the pattern is `\\B(?=(\\d{3})+(?!\\d))`, which matches whitespace characters only between groups of three digits (with an optional digit before or after the group). **Special JavaScript Feature** The benchmark uses template literals (`${num}`) in some test cases. Template literals are a feature introduced in ECMAScript 2015 that allows embedding expressions inside string literals. **Other Alternatives** If you were to implement this benchmark, you could also consider other approaches, such as: * Using a faster JavaScript engine or compiler * Optimizing the regex pattern for better performance * Implementing the `formatNumber` function using a different language (e.g., C++ or Rust) * Using a benchmarking library like Benchmark.js Keep in mind that the specific implementation and optimizations will depend on your goals, target audience, and performance requirements.
Related benchmarks:
To Formatted Number
Three Digit Validator
test regex vs loop
parseFloat isNaN vs RegEx parseFloat vs Number isNaN
Comments
Confirm delete:
Do you really want to delete benchmark?