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);
var array = [];
array.length = 18
var i = 0;
array[i++] = "p1";
array[i++] = 1;
array[i++] = "p2";
array[i++] = 2;
array[i++] = "p3";
array[i++] = 3;
array[i++] = "p4";
array[i++] = 4;
array[i++] = "p5";
array[i++] = 5;
array[i++] = "p6";
araay[i++] = 6;
array[i++] = "p7";
araay[i++] = 7;
array[i++] = "p8";
array[i++] = 8;
array[i++] = "p9";
array[i++] = 9;
var array = [];
array.length = 18
var i = 0;
array[i++] = "p1";
array[i++] = "1";
array[i++] = "p2";
array[i++] = "2";
array[i++] = "p3";
array[i++] = "3";
array[i++] = "p4";
array[i++] = "4";
array[i++] = "p5";
array[i++] = "5";
array[i++] = "p6";
array[i++] = "6";
array[i++] = "p7";
array[i++] = "7";
array[i++] = "p8";
array[i++] = "8";
array[i++] = "p9";
array[i++] = "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 | |
buffered array integer values | |
buffered array string value |
Test name | Executions per second |
---|---|
dynamic create object integer value | 881376832.0 Ops/sec |
dynamic create array integer value | 42840184.0 Ops/sec |
dynamic create object string value | 876197376.0 Ops/sec |
dynamic create array string value | 43820948.0 Ops/sec |
buffered array integer values | 0.0 Ops/sec |
buffered array string value | 8249500.0 Ops/sec |
Let's break down the benchmark and its test cases.
Benchmark Overview
The benchmark measures the performance of three different approaches to dynamically create an object or array:
var object = {};
).push()
method.Test Cases
Each test case has a unique name that describes its purpose:
The test cases are executed multiple times (the exact number is not specified) and the results are reported in terms of executions per second.
Interpretation
Based on the benchmark results, we can see that:
push()
method is slower than creating an object, but faster than the buffered approach for string values.General Observations
The benchmark highlights the importance of choosing the right data structure and creation method depending on the use case. In general:
var object = {};
) is often the fastest and most efficient way to create objects.push()
method is a convenient way to add elements to an array, but it may incur additional overhead compared to creating the array directly.Overall, this benchmark provides valuable insights into the performance characteristics of different data structure creation methods and can inform design decisions when working with JavaScript.