Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
findCommon elements in th JS
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:141.0) Gecko/20100101 Firefox/141.0
Browser:
Firefox 141
Operating system:
Linux
Device Platform:
Desktop
Date tested:
8 months ago
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
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();