var numbers = [1, 12, 123, 1234, 12345, 123456, 1234567, 12345678, 123456789];
var strNumbers = ["1", "12", "123", "1234", "12345", "123456", "1234567", "12345678", "123456789"];
numbers.forEach(n => n.toString());
strNumbers.forEach(n => n.toString());
numbers.forEach(n => `${n}`);
strNumbers.forEach(n => `${n}`);
numbers.forEach(n => String(n));
strNumbers.forEach(n => String(n));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
toString() | |
toString() on strings | |
String literals | |
String literals on strings | |
String() | |
String() on strings |
Test name | Executions per second |
---|---|
toString() | 6420153.5 Ops/sec |
toString() on strings | 8468623.0 Ops/sec |
String literals | 12313887.0 Ops/sec |
String literals on strings | 9138909.0 Ops/sec |
String() | 740127.2 Ops/sec |
String() on strings | 716435.3 Ops/sec |
Let's break down the provided JSON data and explain what is tested in each benchmark.
Benchmark Overview
MeasureThat.net is a website where users can create and run JavaScript microbenchmarks to compare the performance of different approaches on their browsers. The current benchmark consists of six test cases that measure the performance of various string-related operations: toString()
, String()
, and using template literals (${}
).
Benchmark Definitions
The provided JSON data defines two main sections:
Test Case Explanations
Here's a brief explanation of each test case:
toString()
: Measures the performance of calling toString()
on numeric values using the forEach()
method.toString() on strings
: Similar to the previous test case, but measures the performance of calling toString()
on string literals using the forEach()
method.String literals
: Measures the performance of using template literals (${}
) on numeric values within a forEach()
loop.String literals on strings
: Similar to the previous test case, but measures the performance of using template literals (${}
) on string literals within a forEach()
loop.String()
: Measures the performance of calling String()
on numeric values using the forEach()
method.String() on strings
: Similar to the previous test case, but measures the performance of calling String()
on string literals using the forEach()
method.Library and Syntax Considerations
None of the provided test cases use any external libraries or JavaScript features that are not part of the standard language specification.
Browser Comparison
The latest benchmark results show that Chrome 95 is the fastest browser for all six test cases, with varying performance across different test cases. The exact rankings may depend on the specific implementation details and caching behavior of each browser.
Alternative Approaches
If you're interested in exploring alternative approaches or optimizations for string-related operations, here are a few options to consider:
Array.prototype.map()
instead of forEach()
, as it might provide better performance.String.prototype.forEach()
or Array.prototype.forEach.call()
to work around browser limitations.Keep in mind that these alternatives may require significant changes to your codebase or have trade-offs in terms of complexity, compatibility, or maintainability.