Skip to content

Instantly share code, notes, and snippets.

@Rokt33r
Last active August 26, 2016 11:13
Show Gist options
  • Save Rokt33r/1d894f341e63eb301b196434d1f75792 to your computer and use it in GitHub Desktop.
Save Rokt33r/1d894f341e63eb301b196434d1f75792 to your computer and use it in GitHub Desktop.
find-by-key
var crypto = require('crypto')
function keygen () {
return crypto.randomBytes(6).toString('hex')
}
var _ = require('lodash')
var arr = []
for (var i = 0; i < 1000; i++) {
arr.push({
key: keygen(),
index: i
})
if (i === 500) {
arr.push({
key: 'target',
index: 1001
})
}
}
var obj = {}
arr.forEach((item) => {
obj[item.key] = {
index: i
}
})
var Immutable = require('immutable')
var immutableMap = Immutable.fromJS(obj)
var nativeMap = new Map()
arr.forEach((item) => {
nativeMap.set(item.key, {
index: i
})
})
var startTime1 = process.hrtime()
var target1 = _.find(arr, {key: 'target'})
var endTime1 = process.hrtime(startTime1)
console.log('current approach : ', endTime1[0] * 1000000 + endTime1[1] / 1000)
var startTime2 = process.hrtime()
var target2 = obj['target']
var endTime2 = process.hrtime(startTime2)
console.log('key/value obj : ', endTime2[0] * 1000000 + endTime2[1] / 1000)
var startTime3 = process.hrtime()
var target3 = immutableMap.get('target')
var endTime3 = process.hrtime(startTime3)
console.log('immutable map : ', endTime3[0] * 1000000 + endTime3[1] / 1000)
var startTime4 = process.hrtime()
var target4 = nativeMap.get('target')
var endTime4 = process.hrtime(startTime4)
console.log('native map : ', endTime4[0] * 1000000 + endTime4[1] / 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment