var array = Array.from({ length: 10000 }, () => Math.floor(Math.random() * 10000))
var array = Array.from({ length: 10000 }, () => Math.floor(Math.random() * 10000))
array.forEach(n => {
const repeat = Math.sqrt(n);
const result = [[], []];
for (let i = 1; i <= repeat; i++) {
if(n % i === 0) {
result.push(i);
if (i !== repeat) result.push(n / i);
}
}
result.sort().reverse()[0];
}
)
array.forEach(n => {
const repeat = Math.sqrt(n);
const result = [[], []];
for (let i = 1; i <= repeat; i++) {
if(n % i === 0) {
result.push(i);
if (i !== repeat) result.push(n / i);
}
}
result.sort()[result.length-1];
}
)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
reverse | |
sort |
Test name | Executions per second |
---|---|
reverse | 30.1 Ops/sec |
sort | 30.3 Ops/sec |
The benchmark defined on MeasureThat.net involves a comparison between two different methods of retrieving the maximum divisor of a number: one method uses sorting followed by reversing the sorted results, while the other method employs a direct sort of the results. Both methods iterate through the divisors of numbers generated in an array using the forEach
method.
Input Preparation:
Both benchmark tests start by preparing an array of 10,000 random integers, each between 0 and 10,000. This setup provides uniform input for both test cases.
Test Cases:
Sort and Reverse Method:
array.forEach(n => {
const repeat = Math.sqrt(n);
const result = [[], []];
for (let i = 1; i <= repeat; i++) {
if(n % i === 0) {
result.push(i);
if (i !== repeat) result.push(n / i);
}
}
result.sort().reverse()[0];
});
This approach sorts the divisors of n
, reverses the sorted array to retrieve the largest divisor, and accesses the first index.
Sort Method:
array.forEach(n => {
const repeat = Math.sqrt(n);
const result = [[], []];
for (let i = 1; i <= repeat; i++) {
if(n % i === 0) {
result.push(i);
if (i !== repeat) result.push(n / i);
}
}
result.sort()[result.length-1];
});
In this approach, the divisors are sorted, and the last element of the sorted array (which is the largest) is directly accessed.
Pros of Sort and Reverse:
Cons of Sort and Reverse:
Pros of Sort only:
Cons of Sort only:
The benchmark results show that both methods executed approximately 30 times per second. The sorting followed by accessing the last element performed slightly better, with 30.30 executions per second compared to the sort-and-reverse method’s 30.09 executions per second.
Complexity: Both methods involve a time complexity of O(n log n) due to sorting. Alternative algorithms for finding the maximum divisor could potentially reduce this complexity (linear search or heap data structures), especially in specific circumstances.
Alternative Libraries:
While the benchmark does not utilize external libraries, tools like Lodash or Underscore can offer utility functions for dealing with arrays, including sorting and manipulating data efficiently. However, this might add overhead and complexity if not used properly.
Special JavaScript Features:
The benchmark uses ES6 features such as const
and arrow functions, which enhance readability and maintainability of the code. The Array.from
method generates a new array from the given iterable or array-like structure, which is a modern JavaScript feature that makes it easier to create arrays from other data types.
This benchmark provides a practical introduction to performance testing in JavaScript. Understanding the underlying algorithms and comparing them helps optimize performance for similar scenarios.