Skip to content

Instantly share code, notes, and snippets.

@limarc
Last active November 8, 2016 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save limarc/0192487da5863a60d6cd96c74e58432b to your computer and use it in GitHub Desktop.
Save limarc/0192487da5863a60d6cd96c74e58432b 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;
}
// 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;
}
// new Set
function uniqueSet(v) {
return Array.from(new Set(v));
}
// Get random number
function random(to) {
return Math.floor(Math.random() * to);
}
// Generate random
function getRandomArray(length, maxNumber) {
var arr = [];
for (var i = 0; i < length; i++) {
arr.push(random(maxNumber));
}
return arr;
}
// High-resolution time
function hrtime(note) {
var end = process.hrtime(start)[1] / 1000000;
console.log('\n\033[33m' + note + '\033[37m');
console.log('Time', process.hrtime(start)[0] + 's, ' + end.toFixed(3) + 'ms');
start = process.hrtime();
}
// Test func.
function testcase(name, source, fn) {
// Reset
gc();
start = process.hrtime();
// var startMemory = process.memoryUsage().heapUsed;
// Testing
for (var i = 0; i < limit; i++) {
fn(source);
}
// Result
hrtime(name);
console.log('MemoryUsage', (process.memoryUsage().heapUsed - startMemory) + 'b');
}
// Params
var start = [];
var startMemory = process.memoryUsage().heapUsed;
var source = getRandomArray(1000, 10);
var limit = 500000;
// Output info params
console.log('Iteration — ' + limit);
// Testcases
testcase('Testcase#object', source, uniqueObject);
testcase('Testcase#sort', source, uniqueSort);
testcase('Testcase#Set', source, uniqueSet);
@limarc
Copy link
Author

limarc commented Nov 8, 2016

Run

node --expose-gc ./sort-vs-object.js

@s9k
Copy link

s9k commented Nov 8, 2016

Изменения и дополнения:

function uniqueObject2(array) {
    var hash = Object.create(null);
    var length = array.length;
    var result = [];
    var value;

    for (var i = 0; i < length; i++) {
        value = array[i];
        if (!hash[value]) {
            hash[value] = true;
            result.push(value);
        }
    }

    return result;
}

function random(to) {
    return Math.floor(Math.random() * to);
}

function getRandomArray(length, maxNumber) {
    var arr = [];
    for (var i = 0; i < length; i++) {
        arr.push(random(maxNumber));
    }
    return arr;
}

var source = getRandomArray(1000, 10);

testcase('Testcase#object2', source, uniqueObject2);

Результат:

Iteration — 500000

Testcase#object
Time 3s, 790.357ms
MemoryUsage 4616592b

Testcase#object2
Time 2s, 580.334ms
MemoryUsage 4985672b

Testcase#sort
Time 22s, 115.686ms
MemoryUsage 3928992b

Testcase#Set
Time 45s, 582.532ms
MemoryUsage 7110944b

PS MemoryUsage показывает неверные данные

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment