function stray_happy(numbers) {
numbers = numbers.sort((a, b) => a - b);
if (numbers[0] !== numbers[1]) {
return numbers[0];
}
else {
return numbers[numbers.length - 1];
}
}
function stray_heretic(numbers) {
return numbers.find(i => numbers.filter(j => j === i).length === 1);
}
function stray_shaye(numbers) {
return +numbers.sort((a, b) => a - b)
.filter((n,i,a) => (i === 0 && a[0] !==a[1]) || (i === a.length-1 && a[a.length-1] !== a[a.length-2]));
}
function stray_user(a) {
return a.find(v => a[0] != a[1] ? v != a[2] : v != a[0])
}
function stray_user_2(a) {
a.find(a[0] != a[1] ? (v, i, a) => v != a[2] : (v, i, a) => v != a[0])
}
function stray_user_3(a) {
var a0 = a[0], a1 = a[1], a2 = a[2];
a.find(a0 != a1 ? (v, i, a) => v != a2 : (v, i, a) => v != a0)
}
function stray_user_4(a) {
var a0 = a[0], a1 = a[1], a2 = a[2];
if (a0 != a1) a.find((v,i,a) => v != a2);
else a.find((v,i,a) => v != a0);
}
var numbers = [1,1,1,1,1,1,1,2,1,1,1,1,1,1,1]
var n = stray_happy(numbers)
var n = stray_heretic(numbers)
var n = stray_shaye(numbers)
var n = stray_user(numbers)
var n = stray_user_2(numbers)
var n = stray_user_3(numbers)
var n = stray_user_4(numbers)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
stray_happy | |
stray_heretic | |
stray_shaye | |
stray_user | |
stray_user_2 | |
stray_user_3 | |
stray_user_4 |
Test name | Executions per second |
---|---|
stray_happy | 1793652.2 Ops/sec |
stray_heretic | 61335.9 Ops/sec |
stray_shaye | 535139.2 Ops/sec |
stray_user | 7278276.0 Ops/sec |
stray_user_2 | 3486960.0 Ops/sec |
stray_user_3 | 3383871.8 Ops/sec |
stray_user_4 | 7610989.5 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Definition
The benchmark is based on a JavaScript function that finds the stray element in an array of numbers. The function takes an array numbers
as input and returns the stray element.
Individual Test Cases
There are six individual test cases, each testing a different implementation of finding the stray element:
stray_happy
: uses a simple sorting approach to find the stray element.stray_heretic
: uses the find()
method with a callback function to find the stray element.stray_shaye
: uses a combination of sorting and filtering to find the stray element.stray_user
: uses a custom implementation with multiple conditions to find the stray element.stray_user_2
: similar to stray_user
, but with some differences in the logic.stray_user_3
and stray_user_4
: two more custom implementations with slightly different approaches.Options Compared
The benchmark is comparing the performance of six different approaches to finding the stray element:
stray_happy
)find()
method (stray_heretic
)stray_shaye
)stray_user
and its variants)Pros and Cons
Here are some pros and cons for each approach:
find()
method (stray_heretic):Performance Results
The benchmark results show the execution speed for each test case on a Macintosh machine running Chrome 75. The top performer is stray_heretic
, followed by stray_shaye
and stray_user_4
. The other variants have slower performance, with stray_happy
being one of the slowest.
Overall, the benchmark suggests that using the find()
method can be a good approach for finding stray elements in large datasets, while custom implementations with multiple conditions may offer better performance for specific use cases. However, it's essential to note that these results may vary depending on the specific input data and hardware configuration.