Skip to content

Instantly share code, notes, and snippets.

@lahmatiy
Forked from limarc/sort-vs-object.js
Last active November 8, 2016 13:37
Show Gist options
  • Save lahmatiy/bcd080035152daa4d04bd84a9e7effd6 to your computer and use it in GitHub Desktop.
Save lahmatiy/bcd080035152daa4d04bd84a9e7effd6 to your computer and use it in GitHub Desktop.
// Unique with use object
function uniqueObject(array) {
var hash = Object.create(null);
var result = [];
var value;
for (var i = 0; i < array.length; i++) {
value = array[i];
if (!hash[value]) {
hash[value] = true;
result.push(value);
}
}
return result;
}
function uniqueObjectR(v) {
var used = {};
var res = [];
for (var i = 0; i < v.length; i++) {
var value = v[i];
if (!used.hasOwnProperty(value)) {
used[value] = true;
res.push(value);
}
}
return res;
}
// Unique with sort
function uniqueSort(v) {
var y = [];
var c = v.slice().sort(function(a, b) {
return a - b;
});
var lastPush = c[0];
y.push(lastPush);
for (var j = 0; j <= c.length; j++) {
var value = c[j];
if (j > 0 && lastPush < value) {
lastPush = value;
y.push(value);
}
}
return y;
}
function uniqueSortR(v) {
var res = v.slice().sort(function(a, b) {
return a - b;
});
var last = res[0];
for (var i = 1, k = 1; i < res.length; i++) {
var value = res[i];
if (last !== value) {
last = value;
res[k++] = value;
}
}
res.length = k;
return res;
}
// new Set
function uniqueSet(v) {
return Array.from(new Set(v));
}
function uniqueSetR(v) {
return [...new Set(v)];
}
function uniqueSetR2(v) {
var used = new Set();
var res = [];
for (var i = 0; i < v.length; i++) {
var value = v[i];
if (!used.has(value)) {
used.add(value);
res.push(value);
}
}
return res;
}
// Get random number
function random(to) {
return Math.floor(Math.random() * to);
}
// Generate random
function getRandomArray(length, maxNumber) {
var arr = new Array(length);
for (var i = 0; i < length; i++) {
arr[i] = random(maxNumber);
}
return arr;
}
// High-resolution time
function hrtime(note, start) {
var timeDiff = process.hrtime(start);
console.log('\n\033[33m' + note + '\033[37m');
console.log('Time', parseInt(timeDiff[0] * 1e3 + timeDiff[1] / 1e6) + 'ms');
}
// Test func.
function testcase(name, source, fn) {
// Reset
gc();
var mem = process.memoryUsage().heapUsed;
var start = process.hrtime();
// Testing
for (var i = 0; i < limit; i++) {
fn(source);
}
// Result
hrtime(name, start);
console.log('MemoryUsage', String(process.memoryUsage().heapUsed - mem).replace(/(\d{3})(\d{3})?$/, function(m, a, b){
return ' ' + a + (b ? ' ' + b : '');
}) + ' bytes');
// console.log(fn(source), source);
}
// Params
var source = getRandomArray(1000, 10);
var limit = 50000;
// Output info params
console.log('Iteration — ' + limit);
// Testcases
testcase('Testcase#object', source, uniqueObject);
testcase('Testcase#objectR', source, uniqueObjectR);
testcase('Testcase#sort', source, uniqueSort);
testcase('Testcase#sortR', source, uniqueSortR);
testcase('Testcase#Set', source, uniqueSet);
testcase('Testcase#SetR', source, uniqueSetR);
testcase('Testcase#SetR2', source, uniqueSetR2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment