.DS_Store
# reduce-data-with-javascript
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Plunker</title>
</head>
<body>
<script>
// Allows Plunker to display logs in preview panel
// for better in-browser experience
var originalConsoleLog = console.log
console.log = function () {
originalConsoleLog.apply(console, arguments)
var args = Array.prototype.slice.call(arguments);
document.body.innerText += args.join(' ') + '\n';
document.body.style['fontFamily'] = 'monospace';
document.body.style['fontSize'] = '1.5em';
};
</script>
<script src="script.js"></script>
</body>
</html>
var data = [1, 2, 3];
var doubled = data.reduce(function(acc, value) {
acc.push(value * 2);
return acc;
}, []);
var doubleMapped = data.map(function(item) {
return item * 2;
});
var data2 = [1, 2, 3, 4, 5, 6];
var evens = data2.reduce(function(acc, value) {
if (value % 2 === 0) {
acc.push(value);
}
return acc;
}, []);
var evenFiltered = data2.filter(function(item) {
return (item % 2 === 0);
});
var filterMapped = data2.filter(function(value) {
return value % 2 === 0;
}).map(function(value) {
return value * 2;
});
var bigData = [];
for (var i = 0; i < 1000000; i++) {
bigData[i] = i;
}
let filterBegin = Date.now()
var filterMappedBigData = bigData.filter(function(value) {
return value % 2 === 0;
}).map(function(value) {
return value * 2;
});
let filterEnd = Date.now()
let filtertimeSpent = (filterEnd-filterBegin)/1000 + "secs";
let reducedBegin=Date.now();
var reducedBigData = bigData.reduce(function(acc, value) {
if (value % 2 === 0) {
acc.push(value * 2);
}
return acc;
}, []);
let reducedEnd = Date.now();
let reducedtimeSpent = (reducedEnd-reducedBegin)/1000 + " secs";
console.log("filtered Big Data:", filtertimeSpent)
console.log("reduced Big Data:", reducedtimeSpent)