var object = {};
object["p1"] = 1;
object["p2"] = 2;
object["p3"] = 3;
object["p4"] = 4;
object["p5"] = 5;
object["p6"] = 6;
object["p7"] = 7;
object["p8"] = 8;
object["p9"] = 9;
var array = [];
array.push("p1",1);
array.push("p2",2);
array.push("p3",3);
array.push("p4",4);
array.push("p5",5);
array.push("p6",6);
array.push("p7",7);
array.push("p8",8);
array.push("p9",9);
var object = {};
object["p1"] = "1";
object["p2"] = "2";
object["p3"] = "3";
object["p4"] = "4";
object["p5"] = "5";
object["p6"] = "6";
object["p7"] = "7";
object["p8"] = "8";
object["p9"] = "9";
var array = [];
array.push("p1",1);
array.push("p2",2);
array.push("p3",3);
array.push("p4",4);
array.push("p5",5);
array.push("p6",6);
array.push("p7",7);
array.push("p8",8);
array.push("p9",9);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
dynamic create object integer value | |
dynamic create array integer value | |
dynamic create object string value | |
dynamic create array string value |
Test name | Executions per second |
---|---|
dynamic create object integer value | 734201728.0 Ops/sec |
dynamic create array integer value | 16573828.0 Ops/sec |
dynamic create object string value | 162379056.0 Ops/sec |
dynamic create array string value | 18225588.0 Ops/sec |
Let's dive into the explanation of the provided benchmark.
Benchmark Definition
The benchmark is designed to measure the performance difference between creating objects and arrays using dynamic property assignment. The test cases are identical, except for the type of values being assigned: integers or strings.
Options Compared
Two approaches are compared:
object
keyword and assigning properties dynamically using bracket notation (object["pX"] = value;
).array
keyword and pushing elements onto it dynamically using the push()
method (array.push("pX", value);
).Pros and Cons of Each Approach
push()
.Library and Purpose
There are no external libraries used in the benchmark. However, the Array.prototype.push()
method is a part of the JavaScript language standard.
Special JS Feature/ Syntax (None)
No special JavaScript features or syntax are used in this benchmark.
Other Considerations
When writing performance-critical code, it's essential to consider the trade-offs between readability, maintainability, and optimization. In general:
Alternative Benchmarks
Other alternatives for measuring performance differences between dynamic object and array creation include:
These alternative approaches can provide more comprehensive insights into the performance characteristics of dynamic object and array creation, but may require additional setup and configuration.