Created
July 5, 2016 21:07
-
-
Save kessler/10beede508bf0ab88c455d806482ae13 to your computer and use it in GitHub Desktop.
lodash_vs_simple.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const _ = require('lodash') | |
function intersection(source, target) { | |
let result = [] | |
for (let i = source.length - 1; i >= 0; i--) { | |
let value = source[i] | |
if (target.indexOf(value) > - 1) { | |
result.push(value) | |
} | |
} | |
return result | |
} | |
let longArray = [] | |
let shortArray = [] | |
for (let i = 0; i < 1000; i++) { | |
longArray.push(i) | |
if (i % 20 === 0) | |
shortArray.push(i) | |
} | |
console.log('longArray %d', longArray.length) | |
console.log('shortArray %d', shortArray.length) | |
console.time('gt') | |
let result | |
for (let i = 0; i < 10000; i++) { | |
result = intersection(longArray, shortArray) | |
} | |
console.timeEnd('gt') | |
console.log(result.length) | |
console.time('lodash') | |
for (let i = 0; i < 10000; i++) { | |
result = _.intersection(longArray, shortArray) | |
} | |
console.timeEnd('lodash') | |
console.log(result.length) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment