function moreData(arr, left) {
if(left === 0) return arr;
else {
arr.push("Lorem ipsum dolor sit amet, consectetur adipiscing");
return moreData(arr, left - 1);
}
}
function makeTestData() { return moreData([], 40); }
makeTestData().join('-')
JSON.stringify(makeTestData());
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
join | |
JSON.stringify |
Test name | Executions per second |
---|---|
join | 679954.2 Ops/sec |
JSON.stringify | 256216.4 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and discussed.
Benchmark Description
The benchmark is designed to compare two approaches for serializing data: array.join()
and JSON.stringify()
. The test case creates an array of 40 elements with a static string value using the makeTestData
function. This function uses recursion (moreData
) to populate the array, which is then joined together into a single string or serialized as JSON.
Options Compared
Two options are being compared:
join()
method with a specified separator (in this case, an empty string).Pros and Cons
Here are some points to consider for each approach:
Library Used
In this benchmark, the JSON
object is used. This is a built-in JavaScript library that provides methods for parsing and generating JSON data.
Special JS Feature or Syntax
There are no special JavaScript features or syntaxes being tested in this benchmark.
Alternative Approaches
Some alternative approaches to serializing data include:
join()
or stringify()
, you can use template literals (''
) to concatenate strings with variables.Benchmark Preparation Code
The provided script preparation code is quite interesting. It uses recursion (moreData
) to populate an array of 40 elements with a static string value. This can help reduce the execution time compared to creating and iterating over the array in a single pass.
function moreData(arr, left) {
if (left === 0) return arr;
else {
arr.push("Lorem ipsum dolor sit amet, consectetur adipiscing");
return moreData(arr, left - 1);
}
}
function makeTestData() {
return moreData([], 40);
}