var arr = ['123', undefined, false, 'abc', 'flex', 'flex-row', 'items-center justify-center', undefined, undefined, false];
JSON.stringify(arr)
arr.filter(Boolean).join(' ')
arr.toString()
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
stringify | |
join | |
toString |
Test name | Executions per second |
---|---|
stringify | 2178574.5 Ops/sec |
join | 1984807.5 Ops/sec |
toString | 2884137.8 Ops/sec |
Benchmark Overview
The provided JSON represents a JavaScript microbenchmarking test case on MeasureThat.net. The benchmark compares the performance of three different approaches to convert an array into a string: JSON.stringify()
, Array.prototype.join()
with Boolean
filtering, and toString()
.
Approaches Compared
with
Booleanfiltering: This approach uses the
join()method to concatenate the elements of the array into a single string, separated by spaces. The
Boolean` filtering ensures that only truthy values are included in the resulting string.Pros and Cons of Each Approach
with
Boolean` filtering:join()
with filtering.Library Usage
None of the benchmarked methods rely on any external libraries. However, JSON.stringify()
uses the built-in JSON
object and serialization algorithm.
Special JS Features/Syntax
None mentioned in this specific test case.
Alternative Approaches
If you need to convert an array into a string and require more control over the output or performance, consider the following alternatives:
Array.prototype.map()
and String.prototype.join()
to create a string representation of the array elements.Keep in mind that these alternatives may require additional code and consideration to ensure optimal performance and compatibility with various browsers.