<script>
function number_sort(ary) {
return ary.sort(function(a, b) {
return a - b;
});
}
function string_sort(ary) {
return ary.sort(function(a, b) {
return a.localeCompare(b);
});
}
</script>
var numbers = [];
for (var i = 0; i < 10000; i++) {
numbers[i] = Math.round(Math.random() * 1000000);
}
var strings = [];
for (var i = 0; i < 10000; i++) {
strings[i] = Math.round(Math.random() * 1000000).toString();
}
number_sort(numbers.slice(0));
string_sort(strings.slice(0));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Built-in Sort (numbers) | |
Built-in Sort (strings) |
Test name | Executions per second |
---|---|
Built-in Sort (numbers) | 1069.8 Ops/sec |
Built-in Sort (strings) | 56.2 Ops/sec |
Overview of the Benchmark
The provided JSON represents a JavaScript benchmark test case on MeasureThat.net. The benchmark measures the performance of two built-in sorting functions in JavaScript: sort()
for numbers and localeCompare()
for strings.
Script Preparation Code
The script preparation code generates two arrays, numbers
and strings
, each containing 10,000 random integers or strings between 0 and 1,000,000. These arrays are used as input to the sorting functions being benchmarked.
Html Preparation Code
The HTML preparation code defines two JavaScript functions: number_sort()
and string_sort()
. These functions use the built-in sort()
method for numbers and localeCompare()
method for strings, respectively. The slice(0)
method is used to create a shallow copy of the input arrays.
Individual Test Cases
There are two individual test cases:
numbers
array.strings
array.Options Compared
The two options being compared are:
sort()
method, which is a part of the JavaScript standard library.Pros and Cons of Built-in Sort
Pros and Cons of Custom Implementation
Library and Purpose
The localeCompare()
method is a part of the JavaScript standard library, specifically introduced in ECMAScript 5 (ES5). It compares two strings using their locale-specific collation rules.
Special JS Feature or Syntax
There are no special JS features or syntax used in this benchmark. The code only uses standard JavaScript language constructs and built-in methods.
Other Alternatives
Some alternative sorting algorithms that could be compared in a benchmark include:
These alternatives could be used to compare their performance against the built-in sort()
method and custom implementation in a benchmark test case like this one.