Last active
April 13, 2021 15:26
-
-
Save valeksiev/c46bc2594b8941843f3794c26cef99b9 to your computer and use it in GitHub Desktop.
forEach vs filter + concat vs filter + splat (http://jsbench.github.io/#c46bc2594b8941843f3794c26cef99b9) #jsbench #jsperf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>forEach vs filter + concat vs filter + splat</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
<script src="./suite.js"></script> | |
</head> | |
<body> | |
<h1>Open the console to view the results</h1> | |
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
suite.add("forEach", function () { | |
/* forEach */ | |
const params = 'gaa_at=sdfsdf&gaa_ts=sdf&sdfsdf=sdf&gaa_n=bal&asdf=adf&gaa_sig=mql&asd=bsd'.split('&'); | |
let queryParams = []; | |
params.forEach(param => { | |
if ( | |
param.includes('gaa_at=') || | |
param.includes('gaa_ts=') || | |
param.includes('gaa_n=') || | |
param.includes('gaa_sig=') | |
){ | |
queryParams.push(param); | |
} | |
}); | |
}); | |
suite.add("filter test", function () { | |
/* filter test */ | |
const params = 'gaa_at=sdfsdf&gaa_ts=sdf&sdfsdf=sdf&gaa_n=bal&asdf=adf&gaa_sig=mql&asd=bsd'.split('&'); | |
const regex = /(gaa_(at|ts|n|sig)=)/; | |
let queryParams = []; | |
const gaaParams = params.filter((param) => regex.test(param)); | |
queryParams = queryParams.concat(gaaParams); | |
}); | |
suite.add("filter + splat", function () { | |
/* filter + splat */ | |
const params = 'gaa_at=sdfsdf&gaa_ts=sdf&sdfsdf=sdf&gaa_n=bal&asdf=adf&gaa_sig=mql&asd=bsd'.split('&'); | |
const regex = /^gaa_(at|ts|n|sig)=/; | |
let queryParams = []; | |
const gaaParams = params.filter((param) => regex.test(param)); | |
queryParams = [...queryParams, ...gaaParams]; | |
}); | |
suite.add("filter match", function () { | |
/* filter match */ | |
const params = 'gaa_at=sdfsdf&gaa_ts=sdf&sdfsdf=sdf&gaa_n=bal&asdf=adf&gaa_sig=mql&asd=bsd'.split('&'); | |
const regex = /^gaa_(at|ts|n|sig)=/; | |
let queryParams = []; | |
const gaaParams = params.filter((param) => param.match(regex)); | |
queryParams = queryParams.concat(gaaParams); | |
}); | |
suite.add("forEach + regex", function () { | |
/* forEach + regex*/ | |
const params = 'gaa_at=sdfsdf&gaa_ts=sdf&sdfsdf=sdf&gaa_n=bal&asdf=adf&gaa_sig=mql&asd=bsd'.split('&'); | |
const regex = /(gaa_(at|ts|n|sig)=)/; | |
let queryParams = []; | |
params.forEach(param => { | |
if (regex.test(param)){ | |
queryParams.push(param); | |
} | |
}); | |
}); | |
suite.on("cycle", function (evt) { | |
console.log(" - " + evt.target); | |
}); | |
suite.on("complete", function (evt) { | |
console.log(new Array(30).join("-")); | |
var results = evt.currentTarget.sort(function (a, b) { | |
return b.hz - a.hz; | |
}); | |
results.forEach(function (item) { | |
console.log((idx + 1) + ". " + item); | |
}); | |
}); | |
console.log("forEach vs filter + concat vs filter + splat"); | |
console.log(new Array(30).join("-")); | |
suite.run(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment