Skip to content

Instantly share code, notes, and snippets.

@compilerexe
Created January 15, 2018 03:28
Show Gist options
  • Save compilerexe/559632c4d56d80c2cb8953591cb39c31 to your computer and use it in GitHub Desktop.
Save compilerexe/559632c4d56d80c2cb8953591cb39c31 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/qeneyin
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
</head>
<body>
<script id="jsbin-javascript">
var iterations = 100000
var items = []
console.log('/*--------------------*/')
console.time('#1')
for (var i = 0; i < iterations; i++) {
items.push({id: i, number: parseInt(Math.random() * 1000).toFixed(0)})
}
console.log('Add objects to array completed in ')
console.timeEnd('#1')
console.log('/*--------------------*/')
console.time('#2')
var uniqItems = _.uniq(items, function (item) {
return item.number
})
console.log('result filter items : ', uniqItems.length)
console.log('Called _.uniq completed in ')
console.timeEnd('#2')
console.log('/*--------------------*/')
console.time('#3')
var unique = {}
var distinct = []
for (var k in items) {
if (typeof(unique[items[k].number]) === 'undefined') {
distinct.push({id: items[k].id, number: items[k].number})
unique[items[k].number] = true
}
}
console.log('result filter items : ', distinct.length)
console.log('Javascript uniqe items completed in ')
console.timeEnd('#3')
console.log('/*--------------------*/')
</script>
<script id="jsbin-source-javascript" type="text/javascript">var iterations = 100000
var items = []
console.log('/*--------------------*/')
console.time('#1')
for (var i = 0; i < iterations; i++) {
items.push({id: i, number: parseInt(Math.random() * 1000).toFixed(0)})
}
console.log('Add objects to array completed in ')
console.timeEnd('#1')
console.log('/*--------------------*/')
console.time('#2')
var uniqItems = _.uniq(items, function (item) {
return item.number
})
console.log('result filter items : ', uniqItems.length)
console.log('Called _.uniq completed in ')
console.timeEnd('#2')
console.log('/*--------------------*/')
console.time('#3')
var unique = {}
var distinct = []
for (var k in items) {
if (typeof(unique[items[k].number]) === 'undefined') {
distinct.push({id: items[k].id, number: items[k].number})
unique[items[k].number] = true
}
}
console.log('result filter items : ', distinct.length)
console.log('Javascript uniqe items completed in ')
console.timeEnd('#3')
console.log('/*--------------------*/')</script></body>
</html>
var iterations = 100000
var items = []
console.log('/*--------------------*/')
console.time('#1')
for (var i = 0; i < iterations; i++) {
items.push({id: i, number: parseInt(Math.random() * 1000).toFixed(0)})
}
console.log('Add objects to array completed in ')
console.timeEnd('#1')
console.log('/*--------------------*/')
console.time('#2')
var uniqItems = _.uniq(items, function (item) {
return item.number
})
console.log('result filter items : ', uniqItems.length)
console.log('Called _.uniq completed in ')
console.timeEnd('#2')
console.log('/*--------------------*/')
console.time('#3')
var unique = {}
var distinct = []
for (var k in items) {
if (typeof(unique[items[k].number]) === 'undefined') {
distinct.push({id: items[k].id, number: items[k].number})
unique[items[k].number] = true
}
}
console.log('result filter items : ', distinct.length)
console.log('Javascript uniqe items completed in ')
console.timeEnd('#3')
console.log('/*--------------------*/')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment