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的更多相关文章

  1. [Javascript] Advanced Reduce: Common Mistakes

    Take away: Always check you ruturn the accumulator Always pass in the inital value var data = [" ...

  2. JavaScript中reduce()方法

    原文  http://aotu.io/notes/2016/04/15/2016-04-14-js-reduce/   JavaScript中reduce()方法不完全指南 reduce() 方法接收 ...

  3. [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 ...

  4. [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 ...

  5. [Javascript] Advanced Reduce: Composing Functions with Reduce

    Learn how to use array reduction to create functional pipelines by composing arrays of functions. co ...

  6. javascript之reduce()方法的使用

    以前看到reduce方法,总是看得我头皮发麻,今天无意间又遇到他了,于是学习了下,接触之后,觉得这个方法还挺好用的,在很多地方都可以派上用场,比如,数组中元素求和.数组去重.求数组中的最大值或最小值等 ...

  7. JavaScript map reduce

    23333333333333 map var s = []; for(let i=0;i<10;i++){ s.push(i); } function pow(x){ return x*x; } ...

  8. JavaScript Array Reduce用于数组求和

    需求一 假设有一个数组,需要对其中的元素进行求和. const numbers = [1, -1, 2, 3]; 传统写法,使用for循环求和 const numbers = [1, -1, 2, 3 ...

  9. JavaScript 中 reduce去重方法

    过去有很长一段时间,我一直很难理解 reduce() 这个方法的具体用法,平时也很少用到它.事实上,如果你能真正了解它的话,其实在很多地方我们都可以用得上,那么今天我们就来简单聊聊 JS 中 redu ...

随机推荐

  1. Cacti添加threshold、monitor和setting

    Cacti版本:Version 0.8.8b 一.插件介绍: monitor:通过简单明了的图标提供服务器的运行状态 settings:给不同的插件提供一些共用的信息,如邮件信息,dns信息thold ...

  2. js bind

    1.作用 函数的bind方法用于将函数体内的this绑定到某个对象,然后返回一个新函数. //bind 相比于call apply   this 都等于 obj;   bind是产生一个新的函数 不执 ...

  3. MYSQL :逗号分隔串表,分解成竖表

    DROP TEMPORARY TABLE IF EXISTS Temp_Num ; CREATE TEMPORARY TABLE Temp_Num ( xh INT PRIMARY KEY ); -- ...

  4. underscorejs-reduce学习

    2.3 reduce 2.3.1 语法: _.reduce(list, iteratee, [memo], [context]) 2.3.2 说明: reduce方法把list中元素归结为一个单独的数 ...

  5. php网站判断用户是否是手机访问的方法

    PHP网站判断用户是否用手机访问,如果是手机的话,就跳转到指定的手机友好页面.随着移动设备的普及,网站也会迎来越来越多移动设备的访问.用适应PC的页面,很多时候对手机用户不友好,那么有些时候,我们需要 ...

  6. java常用用代码

    /** *Java获取IP代码 */ import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.ev ...

  7. LightOJ 1317 第六周比赛A题

    A - A Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu   Description Y ...

  8. 3527: [Zjoi2014]力

    题目大意:给出n个数qi,定义 Fj为        令 Ei=Fi/qi,求Ei. 设A[i]=q[i],B[i]=1/(i^2). 设C[i]=sigma(A[j]*B[i-j]),D[i]=si ...

  9. 多目标遗传算法 ------ NSGA-II (部分源码解析)README 算法的部分英文解释

    This is the Readme file for NSGA-II code. About the Algorithm--------------------------------------- ...

  10. NOIP[2015] 运输计划

    传送门 题目描述 Description 公元 2044 年,人类进入了宇宙纪元.L 国有 n 个星球,还有 n−1 条双向航道,每条航道建立在两个星球之间,这 n−1 条航道连通了 L 国的所有星球 ...