<!--your preparation HTML code goes here-->
/*your preparation JavaScript code goes here
To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/
async function globalMeasureThatScriptPrepareFunction() {
// This function is optional, feel free to remove it.
// await someThing();
}
function result() {
return [{ a: 1, b: "this is the string", c: "Some more strings", d: ["array of strings here", "and here"]}];
}
function result() {
return Array.of({ a: 1, b: "this is the string", c: "Some more strings", d: Array.of("array of strings here", "and here")});
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array_literal | |
arrayOf |
Test name | Executions per second |
---|---|
array_literal | 1441092736.0 Ops/sec |
arrayOf | 1364305664.0 Ops/sec |
The benchmark in question is focused on comparing the performance of two different methods for creating arrays in JavaScript: using an array literal and using the Array.of
method. Below is a detailed explanation of each approach, including their pros and cons, as well as context around the library and features used.
Array Literal:
Test Case:
function result() {
return [{ a: 1, b: "this is the string", c: "Some more strings", d: ["array of strings here", "and here"] }];
}
Description: This method utilizes an array literal, which is a straightforward way to create an array in JavaScript. The notation is concise and allows for easily initializing an array with values directly.
Pros:
Cons:
Array.of:
Test Case:
function result() {
return Array.of({ a: 1, b: "this is the string", c: "Some more strings", d: Array.of("array of strings here", "and here") });
}
Description: Array.of
is a method that creates a new Array instance from a variable number of arguments, regardless of their number or type. It provides an alternative way to create arrays, which can include multiple array instances.
Pros:
Cons:
The benchmark results show the execution speed of both methods:
The results indicate that the array literal method is faster than the Array.of
method in this specific test environment and scenario.
Array.of
method can be preferable when more complex or varying input is required.Array.of
syntax for its explicit nature when constructing arrays.new Array()
constructor to create arrays, but this is generally less preferred due to ambiguity (e.g., new Array(2)
creates an array of length 2, not an array with the single element 2).const newArray = [...existingArray]
), but this is not directly comparable to the initial methods due to its context of use.Overall, the benchmark provides useful insights into the performance of different array creation techniques in JavaScript, which can guide developers in making informed decisions based on their specific use cases.