Skip to content

Instantly share code, notes, and snippets.

@Rokt33r

Rokt33r/find-by-key.js

Last active Aug 26, 2016
Embed
What would you like to do?
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
You can’t perform that action at this time.