Skip to content

Instantly share code, notes, and snippets.

@kessler
Created July 5, 2016 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kessler/10beede508bf0ab88c455d806482ae13 to your computer and use it in GitHub Desktop.
Save kessler/10beede508bf0ab88c455d806482ae13 to your computer and use it in GitHub Desktop.
lodash_vs_simple.js
'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