[Javascript] Introducing Reduce: Common Patterns
Learn how two common array functions - map() and filter() - are syntactic sugar for reduce operations. Learn how to use them, how to compose them, and how using reduce can give you a big performance boost over composing filters and maps over a large data set.
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;
});
About big data:
var bigData = [];
for (var i = 0; i < 1000000; i++) {
bigData[i] = i;
} console.time('bigData');
var filterMappedBigData = bigData.filter(function(value) {
return value % 2 === 0;
}).map(function(value) {
return value * 2;
}); console.timeEnd('bigData'); //79ms console.time('bigDataReduce');
var reducedBigData = bigData.reduce(function(acc, value) {
if (value % 2 === 0) {
acc.push(value * 2);
}
return acc;
}, []);
console.timeEnd('bigDataReduce'); //54ms
Because map and filter each will go thought the array, but reduce only go thought once.
[Javascript] Introducing Reduce: Common Patterns的更多相关文章
- [Javascript] Advanced Reduce: Common Mistakes
Take away: Always check you ruturn the accumulator Always pass in the inital value var data = [" ...
- JavaScript中reduce()方法
原文 http://aotu.io/notes/2016/04/15/2016-04-14-js-reduce/ JavaScript中reduce()方法不完全指南 reduce() 方法接收 ...
- [Javascript] Advanced Reduce: Flatten, Flatmap and ReduceRight
Learn a few advanced reduction patterns: flatten allows you to merge a set of arrays into a single a ...
- [Javascript] Advanced Reduce: Additional Reducer Arguments
Sometimes we need to turn arrays into new values in ways that can't be done purely by passing an acc ...
- [Javascript] Advanced Reduce: Composing Functions with Reduce
Learn how to use array reduction to create functional pipelines by composing arrays of functions. co ...
- javascript之reduce()方法的使用
以前看到reduce方法,总是看得我头皮发麻,今天无意间又遇到他了,于是学习了下,接触之后,觉得这个方法还挺好用的,在很多地方都可以派上用场,比如,数组中元素求和.数组去重.求数组中的最大值或最小值等 ...
- JavaScript map reduce
23333333333333 map var s = []; for(let i=0;i<10;i++){ s.push(i); } function pow(x){ return x*x; } ...
- JavaScript Array Reduce用于数组求和
需求一 假设有一个数组,需要对其中的元素进行求和. const numbers = [1, -1, 2, 3]; 传统写法,使用for循环求和 const numbers = [1, -1, 2, 3 ...
- JavaScript 中 reduce去重方法
过去有很长一段时间,我一直很难理解 reduce() 这个方法的具体用法,平时也很少用到它.事实上,如果你能真正了解它的话,其实在很多地方我们都可以用得上,那么今天我们就来简单聊聊 JS 中 redu ...
随机推荐
- 重新开始学习javase_类再生(类的合成和继承)
一.合成在新类里简单地创建原有类的对象.我们把这种方法叫作“合成” 为进行合成,我们只需在新类里简单地置入对象句柄即可.举个例子来说,假定需要在一个对象里容纳几个 String对象.两种基本数据类型以 ...
- Java笔试知识总结(第一回)
- [LeetCode OJ] Reorder List—Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. /** * Definition for singly-linked list. * str ...
- 花非花-记一次linux上运行时报找不到库函数错误
简介: --->:表示依赖 exe ---> a.so ---> utility.so 问题描述: exe运行起来报a.so中的函数f未定义. 解决过程: 一·nm a.so nm ...
- input border IE6 bug
border:none;与border:0;的区别体现有两点:一是理论上的性能差异二是浏览器兼容性的差异. 1.性能差异[border:0;]把border设为“0”像素虽然在页面上看不见,但按bor ...
- HexColorPicker 让选色变得更简单[for Mac]
开发iOS的筒子看过来,走过路过,一不小心就错过~ Xcode里的颜色选择器,不能让你随意制定十六进制的颜色,让选色变成了一种折磨,然而作为开发者和设计师又得经常要用到. 现在有了HexColorPi ...
- Java学习----方法的重载
一个类中有多个同名的参数不一样的方法. 作用:可以根据不同的条件调用不同的方法. 注意:java不会因为方法的返回类型或者权限的不同而判断为不同的两个方法. public class Student ...
- groovy构建和解析xml文件
原文链接:http://www.ibm.com/developerworks/cn/java/j-pg05199/ 代码示例: 构建xml文件: def static createXmlFile(){ ...
- Thinkphp 文本编辑器
文本编辑器:可以从网上下载---ueditor文件夹里面包含php和utf8-php两个文件夹 平时使用时主要用到获取内容和写入内容两个按钮 获取内容: <!DOCTYPE html PUBLI ...
- 关于$_SERVER 常量 HTTP_X_FORWARDED_HOST与 HTTP_HOST的问题
今天在看ecshop的源码,发现了用$_SERVER['HTTP_X_FORWARDED_HOST']来判断主机的地址,就目前来说很多人都是直接通过$_SERVER['HTTP_HOST']来判断的, ...