[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 ...
随机推荐
- Webview的使用和注意事项
1.webView是一个展示web网页的控件,继承 AbsoluteLayout 2.webview的俩个回调应用层: 1)webViewClient 这个对象的创建 WebViewClient my ...
- java保留两位小数的方法
01.import java.math.BigDecimal; 02.import java.text.DecimalFormat; 03.import java.text.NumberFormat; ...
- 请给出异步加载js方案
请给出异步加载js方案,不少于两种 默认情况javascript是同步加载的,也就是javascript的加载时阻塞的,后面的元素要等待javascript加载完毕后才能进行再加载,对于一些意义不是很 ...
- css position 相对定位
<html> <head> <style type="text/css"> h2.pos_left { position:relative; l ...
- 使用Code::Blocks配置Python编译环境
1.先在CodeBlock中新建C或C++工程. CodeBlock新建工程步骤:File——New——Project——Console applications——C或C++都可——Project所 ...
- JavaScript语言内置对象
String(字符串对象)RegExp(正则表达式对象)Number(数字对象)Math(数学对象)Function(函数对象)Error(异常对象)Date(日期/时间对象)Boolean(布尔对象 ...
- Python二分查找
代码: 时间复杂度:O(log2n) #!/usr/bin/env python #coding:utf-8 import copy from copy import deepcopy ''' def ...
- cf C. Bits
http://codeforces.com/contest/485/problem/C 利用位运算来解决这个题,从L开始,从每一位按位或,知道到达r位置,ans=ans|(1<<k);就可 ...
- poj 1275 Cashier Employment
http://poj.org/problem?id=1275 #include <cstdio> #include <cstring> #include <algorit ...
- 用JvisualVM监视远程tomcat
在tomcat的catcalina.sh 中java_opts 环境变量中添加以下参数: -Dcom.sun.management.jmxremote -Dcom.sun.management.jmx ...