Created
March 30, 2018 16:36
-
-
Save thenickcox/239c49c713887f08546194c41227ae77 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Setup | |
var assignments = []; | |
var _ = require('underscore'); | |
function pluckUnique(arrayOfObj, key) { | |
var pluckedValues = {}; | |
arrayOfObj.forEach(function(obj) { | |
var valAtKey = obj[key]; | |
if (pluckedValues[valAtKey]) { | |
pluckedValues[valAtKey]++; | |
} else { | |
pluckedValues[valAtKey] = 1; | |
} | |
}); | |
return _.keys(pluckedValues); | |
} | |
// Different assignments | |
for (let i = 0; i < 5000; i++) { | |
var assignment = { | |
id: i, | |
assignable_id: 1754593, | |
user_id: i, | |
starts_at: '2018-03-10', | |
ends_at: '2018-04-10', | |
all_day_assignment: true, | |
allocation_mode: 'percent', | |
description: 'A description', | |
status: null, | |
fixed_hours: null, | |
repetition_id: null, | |
resource_request_id: null, | |
} | |
assignments.push(assignment); | |
} | |
// 5000 duplicate assignments | |
for (let i = 0; i < 5000; i++) { | |
var assignment = { | |
id: 5000, | |
assignable_id: 1754593, | |
user_id: i, | |
starts_at: '2018-03-10', | |
ends_at: '2018-04-10', | |
all_day_assignment: true, | |
allocation_mode: 'percent', | |
description: 'A description', | |
status: null, | |
fixed_hours: null, | |
repetition_id: null, | |
resource_request_id: null, | |
} | |
assignments.push(assignment); | |
} | |
console.log('Testing lodash implementation'); | |
console.time('lodash'); | |
var ids = _.uniq(_.pluck(assignments, 'id')); | |
console.timeEnd('lodash'); | |
console.log('Testing optimized version'); | |
console.time('optimized'); | |
var ids2 = pluckUnique(assignments, 'id'); | |
console.timeEnd('optimized'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment