Last active
December 14, 2020 08:33
-
-
Save mmsaffari/e6f583e740e107b4f6eabd655114f35d to your computer and use it in GitHub Desktop.
Unique Property Lister #jsbench #jsperf (http://jsbench.github.io/#e6f583e740e107b4f6eabd655114f35d) #jsbench #jsperf
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>Unique Property Lister #jsbench #jsperf</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
<script src="./suite.js"></script> | |
</head> | |
<body> | |
<h1>Open the console to view the results</h1> | |
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2> | |
</body> | |
</html> |
This file contains 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
"use strict"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
Benchmark.prototype.setup = function () { | |
var array = [{"name":"Joe", "age":17}, {"name":"Bob", "age":17}, {"name":"Carl", "age": 35}]; | |
const _getUnique = (arrayOfObjects, propertyName) => { | |
var unique = []; | |
var distinct = []; | |
for (let i = 0; i < arrayOfObjects.length; i++) { | |
if (!unique[arrayOfObjects[i][propertyName]]) { | |
distinct.push(arrayOfObjects[i][propertyName]); | |
unique[arrayOfObjects[i][propertyName]] = 1; | |
} | |
} | |
return distinct | |
} | |
Array.prototype.distinct = function(item){ | |
var results = []; | |
for (var i = 0, l = this.length; i < l; i++) | |
if (!item){ | |
if (results.indexOf(this[i]) === -1) | |
results.push(this[i]); | |
} else { | |
if (results.indexOf(this[i][item]) === -1) | |
results.push(this[i][item]); | |
} | |
return results; | |
}; | |
const _getUniqueEx=(array, propName)=>{ | |
var results = []; | |
for (var i = 0, l = array.length; i < l; i++){ | |
if (results.indexOf(array[i][propName]) === -1) | |
results.push(array[i][propName]); | |
} | |
return results; | |
} | |
}; | |
suite.add("var unique = [];", function () { | |
var unique = []; | |
var distinct = []; | |
for( let i = 0; i < array.length; i++ ){ | |
if( !unique[array[i].age]){ | |
distinct.push(array[i].age); | |
unique[array[i].age] = 1; | |
} | |
} | |
}); | |
suite.add("var flags = [], output = [], l = array.length, i;", function () { | |
var flags = [], output = [], l = array.length, i; | |
for( i=0; i<l; i++) { | |
if( flags[array[i].age]) continue; | |
flags[array[i].age] = true; | |
output.push(array[i].age); | |
} | |
}); | |
suite.add("const unique= _getUniqueEx(array, 'age')", function () { | |
const unique= _getUniqueEx(array, 'age') | |
}); | |
suite.add("const unique=array.distinct('age')", function () { | |
const unique=array.distinct('age') | |
}); | |
suite.on("cycle", function (evt) { | |
console.log(" - " + evt.target); | |
}); | |
suite.on("complete", function (evt) { | |
console.log(new Array(30).join("-")); | |
var results = evt.currentTarget.sort(function (a, b) { | |
return b.hz - a.hz; | |
}); | |
results.forEach(function (item) { | |
console.log((idx + 1) + ". " + item); | |
}); | |
}); | |
console.log("Unique Property Lister #jsbench #jsperf"); | |
console.log(new Array(30).join("-")); | |
suite.run(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment