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 = new Array(20);
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;
var array = new Array(20);
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 | 20534310.0 Ops/sec |
dynamic create array integer value | 2287949.5 Ops/sec |
dynamic create object string value | 21745594.0 Ops/sec |
dynamic create array string value | 2145392.8 Ops/sec |
buffered array integer values | 3471472.2 Ops/sec |
buffered array string value | 4253700.0 Ops/sec |
Let's dive into the benchmark.
Benchmark Definition
The benchmark measures the performance of creating objects and arrays using different approaches:
object[\"key\"] = value
(dynamic object creation)array.push(value)
followed by array.push(key)
(dynamic array creation)var object = {};
or var array = [];
followed by assigning values to properties (buffered object/array creation)Options Compared
The benchmark compares the performance of three options:
object[\"key\"] = value
array.push(value)
followed by array.push(key)
var object = {}
or var array = [];
and then assigning values to propertiesPerformance Comparison
The benchmark results show that:
array.push(value)
followed by array.push(key)
) is slower than buffered object creation.Why the Performance Difference?
There are several reasons why buffered object/array creation might be faster:
var object = {}
or var array = [];
, the browser can cache the initial allocation and subsequent assignments, reducing the number of memory accesses.Keep in mind that these are general observations and may not apply to all use cases or browsers.
Conclusion
The benchmark highlights the importance of choosing the most efficient approach when creating objects and arrays in your code. Buffered object/array creation seems to offer a performance advantage over dynamic object/array creation, but it's essential to consider the specific requirements of your application and potential trade-offs for other factors like code readability and maintainability.