Intersection filter vs lodash intersection test unsorted array
Date tested:
4 years ago
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36
Test name
Executions per second
filter unsorted
1279263.0 Ops/sec
lodash intersection unsort
1269864.0 Ops/sec
filter sorted
1312277.0 Ops/sec
lodash intersection sorted
838276.4 Ops/sec
Benchmark definition (click to collapse):
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var arr1 = [7, 2, 4, 1, 9, 8, 6, 3, 5] var arr2 = [3, 4, 1, 2];
Tests:
filter unsorted
const result = arr1.filter(el => arr2.includes(el))
lodash intersection unsort
const result = _.intersection(arr1, arr2)
filter sorted
const sortedArr1 = arr1.sort() const sortedArr2 = arr2.sort() const result = sortedArr1.filter(el => sortedArr2.includes(el))
lodash intersection sorted
const sortedArr1 = arr1.sort() const sortedArr2 = arr2.sort() const result = _.intersection(sortedArr1, sortedArr2)
Open this result on MeasureThat.net