Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findCommon elements in th JS
(version: 1)
Comparing performance of:
v1 (Mine) vs v2 (Perplexity) vs v3 (Gemini) vs v4 (ChatGPT)
Created:
8 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
Implement a JavaScript function called findCommon Elements that accepts multiple arrays as arguments and returns a new array containing only the elements that are present in every input array. ``` // Function signature function findCommonElements(...arrays) { // write your code here } // Example input and output: // Input: findCommonElements([1, 2, 3, 4], [2, 4, 6, 8], [2, 4, 101) // Expected Output: [2, 4] ```
Script Preparation code:
// ==== Implementation 1: Your current code ==== function findCommonElements_v1(...arrays) { if (!arrays || !Array.isArray(arrays)) { return []; } const cleanArrays = arrays.map((arr) => arr.filter((item) => !!item)); let unique = new Set(cleanArrays[0]); for (const arr of cleanArrays.slice(1)) { let tmpUnique = new Set([...arr]); for (const item of [...unique]) { if (!tmpUnique.has(item)) { unique.delete(item); } } } return [...unique]; } // ==== Implementation 2 ==== function findCommonElements_v2(...arrays) { if (arrays.length === 0) return []; return [...arrays.reduce((acc, curr) => { const currSet = new Set(curr); return new Set([...acc].filter(el => currSet.has(el))); }, new Set(arrays[0]))]; } // ==== Implementation 3 ==== function findCommonElements_v3(...arrays) { if (!arrays || arrays.length === 0) { return []; } const referenceArray = arrays[0]; const otherArrays = arrays.slice(1); return referenceArray.filter(element => { return otherArrays.every(arr => arr.includes(element)); }); } // ==== Implementation 4 ==== function findCommonElements_v4(...arrays) { if (!arrays || !Array.isArray(arrays) || arrays.length === 0) { return []; } let common = new Set(arrays[0]); for (const arr of arrays.slice(1)) { let currentSet = new Set(arr); common = new Set([...common].filter((val) => currentSet.has(val))); if (common.size === 0) break; } return [...common]; } // ==== Test cases ==== const testCases = [ [[1, 2, 3, 4], [2, 4, 6, 8], [2, 4, 101]], [[1, 2, 3], [1, 2, 4], [1, 5]], [[]], [['a', 'b'], ['b', 'c'], ['b', 'd']], [[1, 2], [3, 4], [5]], ]; // ==== Wrappers for benchmark ==== function runV1() { for (let i = 0; i < testCases.length; i++) findCommonElements_v1(...testCases[i]); } function runV2() { for (let i = 0; i < testCases.length; i++) findCommonElements_v2(...testCases[i]); } function runV3() { for (let i = 0; i < testCases.length; i++) findCommonElements_v3(...testCases[i]); } function runV4() { for (let i = 0; i < testCases.length; i++) findCommonElements_v4(...testCases[i]); }
Tests:
v1 (Mine)
/*When writing async/deferred tests, use `deferred.resolve()` to mark test as done*/ runV1();
v2 (Perplexity)
runV2();
v3 (Gemini)
runV3();
v4 (ChatGPT)
runV4();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
v1 (Mine)
v2 (Perplexity)
v3 (Gemini)
v4 (ChatGPT)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:141.0) Gecko/20100101 Firefox/141.0
Browser/OS:
Firefox 141 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
v1 (Mine)
474506.2 Ops/sec
v2 (Perplexity)
320287.9 Ops/sec
v3 (Gemini)
2394742.5 Ops/sec
v4 (ChatGPT)
435621.7 Ops/sec
Related benchmarks:
Remove by splice vs copyWithin vs filter
Remove by splice vs copyWithin vs filter
Remove by splice vs copyWithin vs filter (readonly-2)
remove els
Array.filter vs push
Remove array items: FindIndex + Splice vs Filter
Methods to remove duplicates object with a key from array
Array remove
findCommon elements in th JS v2
Comments
Confirm delete:
Do you really want to delete benchmark?