var n = 10;
function Person(name, age) {
this.name = name;
this.age = age;
}
var arr = new Array(n);
for (var i = 0; i < n; i++) {
arr[i] = new Person(i + '', i);
}
var arr = [];
for (var i = 0; i < n; i++) {
arr.push(new Person(i + '', i));
}
var arr = [];
for (var i = 0; i < n; i++) {
arr[i] = new Person(i + '', i);
}
var arr = [];
arr.length = n;
for (var i = 0; i < n; i++) {
arr[i] = new Person(i + '', i);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array constructor - 100000 items | |
Array literal - 100000 items | |
Array literal (assign by index) - 100000 items | |
Array literal (explicit length) - 100000 items |
Test name | Executions per second |
---|---|
Array constructor - 100000 items | 872285.1 Ops/sec |
Array literal - 100000 items | 946030.2 Ops/sec |
Array literal (assign by index) - 100000 items | 971685.2 Ops/sec |
Array literal (explicit length) - 100000 items | 915577.5 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
Benchmark Definition
The provided JSON represents a benchmark definition, which outlines the test scenario and configuration. Here's what's tested:
Options Compared
The options being compared are:
new Array()
syntax.push()
method, without explicitly specifying the length.length
property and then pushes elements into it.Pros and Cons
Here's a brief analysis of each option:
Library Used
There is no explicit library mentioned in the benchmark definition. However, it's likely that the Person
class used in the script preparation code is an internal implementation detail, and its actual purpose is not relevant to this benchmark.
Special JavaScript Features/Syntax
The benchmark uses:
name
property of the Person
object.Overall, this benchmark provides insight into the performance differences between various array creation methods in JavaScript. By testing different approaches, developers can gain a better understanding of the most efficient way to create arrays and manage memory in their applications.