function randomString(e) {
e = e || 32;
var t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
a = t.length,
n = "";
for (i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
return n
}
var str1 = randomString(100)
var str2 = randomString(100000)
var result1 = Array.from(str1.slice(1, 3)).reduce(function (previousValue, currentValue, index, array) {
return previousValue + currentValue;
})
var result2 = Array.from(str1.slice(4, 6)).reduce(function (previousValue, currentValue, index, array) {
return previousValue + currentValue;
})
var result3 = Array.from(str1.slice(7, 10)).reduce(function (previousValue, currentValue, index, array) {
return previousValue + currentValue;
})
var result1 = Array.from(str2.slice(1, 30000)).reduce(function (previousValue, currentValue, index, array) {
return previousValue + currentValue;
})
var result2 = Array.from(str2.slice(28999, 69999)).reduce(function (previousValue, currentValue, index, array) {
return previousValue + currentValue;
})
var result3 = Array.from(str2.slice(67000, 99999)).reduce(function (previousValue, currentValue, index, array) {
return previousValue + currentValue;
})
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
slice-1 | |
slice-2 |
Test name | Executions per second |
---|---|
slice-1 | 1402509.4 Ops/sec |
slice-2 | 482.5 Ops/sec |
The JSON benchmark you've provided aims to evaluate the performance of JavaScript operations related to slicing strings and then reducing the results into cumulative outputs. The benchmark specifically covers two sets of test cases focused on different source string lengths and slice ranges.
Test Cases: There are two test cases, labeled slice-1
and slice-2
. Each test measures the performance of slicing strings and applying the reduce
function on the resultant segments.
Source Strings:
str1
: A relatively short random string (100 characters).str2
: A longer random string (100,000 characters).Operations:
slice
method is used to create substrings from the original strings. reduce
method accumulates values of the sliced substrings into a single output.slice-1
The first test case operates on the smaller string str1
and performs three slices:
slice(1, 3)
, slice(4, 6)
, and slice(7, 10)
.
Post-slicing, it uses the reduce
method to concatenate the characters in each sliced string.slice-2
The second test case operates on the larger string str2
and performs three slices:
slice(1, 30000)
, slice(28999, 69999)
, and slice(67000, 99999)
.
Similar to slice-1
, each substring is then reduced to a single result through concatenation.The benchmark results indicate the number of executions per second for each test case when run in a Safari browser on macOS.
Using slice
and reduce
:
reduce
is a native method that is optimized for performance.slice-1
on the smaller string yields much higher performance than slice-2
.General Considerations:
Direct String Concatenation: Instead of using reduce
, a simple loop or join
could be used directly to concatenate characters, which might offer better performance in certain scenarios.
Typed Arrays: In cases where numerical operations are required instead of string manipulation, using typed arrays can significantly enhance performance due to their fixed size and optimized memory access.
Custom Reduce Implementations: Implementing a custom reduce function (especially if it can skip unnecessary operations) might yield better performance for specific cases, particularly when the business logic allows it.
This benchmark provides significant insight into how different string lengths and operations behave under JavaScript's management of memory and execution. By understanding these test outcomes and the inherent pros and cons, developers can make informed choices on string manipulation techniques based on the expected sizes and requirements of their applications.