<!--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 isPrime(num) {
for (let i = 2, s = Math.sqrt(num); i <= s; i++) {
if (num % i === 0) {
return false;
}
}
return num > 1;
}
const size = 10000;
let primeNumbers = [];
for (var i = 0; i < size; i++) {
if (isPrime(i)) {
primeNumbers.push(i);
}
if (primeNumbers.length > 10) {
primeNumbers = [];
}
}
const size = 10000;
const primeNumbers = [];
for (var i = 0; i < size; i++) {
if (isPrime(i)) {
primeNumbers.push(i);
}
if (primeNumbers.length > 10) {
primeNumbers.splice(0, primeNumbers.length);
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Re create array (declared using "let") | |
Empty array (declared using "const") |
Test name | Executions per second |
---|---|
Re create array (declared using "let") | 2973.2 Ops/sec |
Empty array (declared using "const") | 2892.5 Ops/sec |
The benchmark titled "Array creation vs empty values" evaluates two approaches to managing an array of prime numbers in JavaScript. It focuses on the performance of two different techniques for emptying or resetting an array during iteration.
Test Case 1: Recreate array (declared using "let")
const size = 10000;
let primeNumbers = [];
for (var i = 0; i < size; i++) {
if (isPrime(i)) {
primeNumbers.push(i);
}
if (primeNumbers.length > 10) {
primeNumbers = [];
}
}
primeNumbers
) whenever its length exceeds 10. The variable is declared using let
, which allows for reassignment.Test Case 2: Empty array (declared using "const")
const size = 10000;
const primeNumbers = [];
for (var i = 0; i < size; i++) {
if (isPrime(i)) {
primeNumbers.push(i);
}
if (primeNumbers.length > 10) {
primeNumbers.splice(0, primeNumbers.length);
}
}
splice
method to empty the primeNumbers
array whenever its length exceeds 10. The variable is declared using const
, which means it cannot be reassigned, but its contents can still be modified.Recreating the Array (Test Case 1):
Emptying the Array (Test Case 2):
splice
introduces some processing overhead, especially in larger arrays, since it shifts elements in the array when modifying its length.Memory Management: When using let
, recreating the array allows for better memory management when the array is expected to grow considerably; however, it must be balanced against the overhead of constant memory allocation.
Performance Metrics: The benchmark results show a performance difference:
These results suggest that recreating the array may provide a slight performance edge in this specific scenario.
Beyond the methods evaluated in this benchmark, there are other alternatives and techniques for managing arrays in JavaScript:
Using Array Methods: More advanced methods like filter
or reduce
can be employed to create new arrays based on certain conditions without modifying the original array.
Immutable Structures: Libraries such as Immutable.js provide data structures that enable developers to manage state immutably, which can lead to cleaner and more predictable data manipulation at the potential cost of performance due to the overhead of creating new instances.
Typed Arrays: If dealing with numeric data, using Typed Arrays (such as Uint8Array
, Float32Array
, etc.) can provide performance benefits due to more efficient memory usage and optimizations in underlying JavaScript engines.
In conclusion, the benchmark presents a useful analysis of how different approaches to array management can affect performance in JavaScript, while also providing insights into memory management considerations in code. Each method offers trade-offs, so the choice of the array management technique should consider specific use cases and performance requirements.