Skip to content

Instantly share code, notes, and snippets.

@velusgautam
Created October 10, 2018 10:53
Show Gist options
  • Save velusgautam/0ec5c32bbe272a78f4ef400f7b523866 to your computer and use it in GitHub Desktop.
Save velusgautam/0ec5c32bbe272a78f4ef400f7b523866 to your computer and use it in GitHub Desktop.
D3 Extend Performance Check
license: mit
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.3/d3.min.js"></script>
<script src="https://cdn.jsdelivr.net/lodash/4/lodash.min.js"></script>
<meta charset="utf-8">
<title>d3 min, max, extent, sort</title>
</head>
<body>
<script>
var d1 = [10,5,20,30,50,60, 90, 120, 130];
var d2 = [
{"id":"1", "value":10},
{"id":"2", "value":5},
{"id":"3", "value":20},
{"id":"4", "value":30}
];
console.log("min = " + d3.min(d1));
console.log("max = " + d3.max(d1));
console.time("D3 Extend");
console.log("extent = " + d3.extent(d1));
console.timeEnd("D3 Extend");
console.log("sort = " + d1.sort(d3.ascending));
//
// buscando el maximo, sobre d2
// 4 metodos
//
console.time("Math Max");
console.log(Math.max.apply(Math, d2.map(function(d){return d.value;})));
console.timeEnd("Math Max")
console.time("ForLoop Max");
let max = 0, min= 0;
for(let i = 0; i < d1.length; i++) {
if(d1[i] > max) {
max = d1[i]
}
if(d1[i] < min) {
min = d1[i];
}
}
console.log(max, min);
console.timeEnd("ForLoop Max")
console.time("d3 Max");
// usando d3
console.log( d3.max(d2.map(function(d){return d.value;})) );
console.timeEnd("d3 Max")
console.time("Loadash Max");
// usando _ (lodash)
console.log(_.maxBy(d2, 'value').value);
console.timeEnd("Loadash Max");
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment