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])
}
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)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
stray_happy | |
stray_heretic | |
stray_shaye | |
stray_user | |
stray_user_2 |
Test name | Executions per second |
---|---|
stray_happy | 2469699.2 Ops/sec |
stray_heretic | 406825.7 Ops/sec |
stray_shaye | 909649.6 Ops/sec |
stray_user | 3903393.5 Ops/sec |
stray_user_2 | 5016806.5 Ops/sec |
Let's break down the benchmark and its individual test cases.
Benchmark Definition JSON
The benchmark definition is a JSON object that contains the details of the benchmark. Here's what it tells us:
stray_happy
, stray_heretic
, stray_shaye
, and two variations of stray_user
(and stray_user_2
).Script Preparation Code
The script preparation code is the JavaScript code that sets up the test environment. In this case, it defines the numbers
array and creates an instance of each benchmark function.
Individual Test Cases
Each test case represents a single execution of one of the benchmark functions. The test cases are:
stray_happy
stray_heretic
stray_shaye
stray_user
stray_user_2
These test cases use different approaches to find the stray element in the sorted array.
Options Compared
The options compared are:
stray_happy
: uses a simple sort and then checks for the first two elementsstray_heretic
: uses the find
method and filters out elements that match the previous onestray_shaye
: uses a sort, filter, and some clever array manipulation to find the stray elementstray_user
: uses a similar approach to stray_shaye
, but with slightly different syntaxstray_user_2
: another variation of stray_user
with even more complex syntaxPros and Cons
Here's a brief summary of the pros and cons of each approach:
stray_happy
: simple and efficient, but may not work for large arrays.stray_heretic
: uses the find
method, which is a built-in JavaScript function.find
.stray_shaye
: uses a sort and filter, but with some clever array manipulation to find the stray element.stray_user
and stray_user_2
: similar to stray_shaye
, but with different syntax.Libraries Used
None of the benchmark functions use any external libraries. They are all pure JavaScript implementations.
Special JS Features or Syntax
There are no special JavaScript features or syntax used in this benchmark. The test cases only use standard JavaScript methods and operators.
Alternatives
If you want to improve performance or efficiency, you could consider using:
find
.However, keep in mind that these optimizations may make the code more complex and harder to understand.