forEach vs filter/map

JavaScript performance comparison

Test case created by walterra

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const colors = [
  	{
  		color: "red",
  		value: "#f00"
  	},
  	{
  		color: "green",
  		value: "#0f0"
  	},
  	{
  		color: "blue",
  		value: "#00f"
  	},
  	{
  		color: "cyan",
  		value: "#0ff"
  	},
  	{
  		color: "magenta",
  		value: "#f0f"
  	},
  	{
  		color: "yellow",
  		value: "#ff0"
  	},
  	{
  		color: "black",
  		value: "#000"
  	},
  	{
  		color: "red",
  		value: "#f00"
  	},
  	{
  		color: "green",
  		value: "#0f0"
  	},
  	{
  		color: "blue",
  		value: "#00f"
  	},
  	{
  		color: "cyan",
  		value: "#0ff"
  	},
  	{
  		color: "magenta",
  		value: "#f0f"
  	},
  	{
  		color: "yellow",
  		value: "#ff0"
  	},
  	{
  		color: "black",
  		value: "#000"
  	},
  	{
  		color: "red",
  		value: "#f00"
  	},
  	{
  		color: "green",
  		value: "#0f0"
  	},
  	{
  		color: "blue",
  		value: "#00f"
  	},
  	{
  		color: "cyan",
  		value: "#0ff"
  	},
  	{
  		color: "magenta",
  		value: "#f0f"
  	},
  	{
  		color: "yellow",
  		value: "#ff0"
  	},
  	{
  		color: "black",
  		value: "#000"
  	},
  	{
  		color: "red",
  		value: "#f00"
  	},
  	{
  		color: "green",
  		value: "#0f0"
  	},
  	{
  		color: "blue",
  		value: "#00f"
  	},
  	{
  		color: "cyan",
  		value: "#0ff"
  	},
  	{
  		color: "magenta",
  		value: "#f0f"
  	},
  	{
  		color: "yellow",
  		value: "#ff0"
  	},
  	{
  		color: "black",
  		value: "#000"
  	},
  	{
  		color: "red",
  		value: "#f00"
  	},
  	{
  		color: "green",
  		value: "#0f0"
  	},
  	{
  		color: "blue",
  		value: "#00f"
  	},
  	{
  		color: "cyan",
  		value: "#0ff"
  	},
  	{
  		color: "magenta",
  		value: "#f0f"
  	},
  	{
  		color: "yellow",
  		value: "#ff0"
  	},
  	{
  		color: "black",
  		value: "#000"
  	},
  	{
  		color: "red",
  		value: "#f00"
  	},
  	{
  		color: "green",
  		value: "#0f0"
  	},
  	{
  		color: "blue",
  		value: "#00f"
  	},
  	{
  		color: "cyan",
  		value: "#0ff"
  	},
  	{
  		color: "magenta",
  		value: "#f0f"
  	},
  	{
  		color: "yellow",
  		value: "#ff0"
  	},
  	{
  		color: "black",
  		value: "#000"
  	},
  	{
  		color: "red",
  		value: "#f00"
  	},
  	{
  		color: "green",
  		value: "#0f0"
  	},
  	{
  		color: "blue",
  		value: "#00f"
  	},
  	{
  		color: "cyan",
  		value: "#0ff"
  	},
  	{
  		color: "magenta",
  		value: "#f0f"
  	},
  	{
  		color: "yellow",
  		value: "#ff0"
  	},
  	{
  		color: "black",
  		value: "#000"
  	},
  	{
  		color: "red",
  		value: "#f00"
  	},
  	{
  		color: "green",
  		value: "#0f0"
  	},
  	{
  		color: "blue",
  		value: "#00f"
  	},
  	{
  		color: "cyan",
  		value: "#0ff"
  	},
  	{
  		color: "magenta",
  		value: "#f0f"
  	},
  	{
  		color: "yellow",
  		value: "#ff0"
  	},
  	{
  		color: "black",
  		value: "#000"
  	},
  	{
  		color: "red",
  		value: "#f00"
  	},
  	{
  		color: "green",
  		value: "#0f0"
  	},
  	{
  		color: "blue",
  		value: "#00f"
  	},
  	{
  		color: "cyan",
  		value: "#0ff"
  	},
  	{
  		color: "magenta",
  		value: "#f0f"
  	},
  	{
  		color: "yellow",
  		value: "#ff0"
  	},
  	{
  		color: "black",
  		value: "#000"
  	},
  	{
  		color: "red",
  		value: "#f00"
  	},
  	{
  		color: "green",
  		value: "#0f0"
  	},
  	{
  		color: "blue",
  		value: "#00f"
  	},
  	{
  		color: "cyan",
  		value: "#0ff"
  	},
  	{
  		color: "magenta",
  		value: "#f0f"
  	},
  	{
  		color: "yellow",
  		value: "#ff0"
  	},
  	{
  		color: "black",
  		value: "#000"
  	},
  	{
  		color: "red",
  		value: "#f00"
  	},
  	{
  		color: "green",
  		value: "#0f0"
  	},
  	{
  		color: "blue",
  		value: "#00f"
  	},
  	{
  		color: "cyan",
  		value: "#0ff"
  	},
  	{
  		color: "magenta",
  		value: "#f0f"
  	},
  	{
  		color: "yellow",
  		value: "#ff0"
  	},
  	{
  		color: "black",
  		value: "#000"
  	},
  	{
  		color: "red",
  		value: "#f00"
  	},
  	{
  		color: "green",
  		value: "#0f0"
  	},
  	{
  		color: "blue",
  		value: "#00f"
  	},
  	{
  		color: "cyan",
  		value: "#0ff"
  	},
  	{
  		color: "magenta",
  		value: "#f0f"
  	},
  	{
  		color: "yellow",
  		value: "#ff0"
  	},
  	{
  		color: "black",
  		value: "#000"
  	},
  ]

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in Firefox 62.0.0 / Windows 10 0.0.0
Test Ops/sec
forEach inline
const transformed = [];
colors.forEach(d => { if (d.color === 'green') transformed.push(d.value); });
pending…
filter/map func
const filterFunc = d => d.color === 'green';
const mapFunc = d => d.value;
const transformed = colors.filter(filterFunc).map(mapFunc);
pending…
filter/map inline
const transformed = colors.filter(d => d.color === 'green').map(d => d.value);
pending…
for loop
const transformed = [];
for (let i = 0; i < colors.length; i++) {
  const c = colors[i];
  if (c.color === 'green') {
    transformed.push(c.value);
  }
}
pending…
forEach func
const transformed = [];
const func = d => { if (d.color === 'green') transformed.push(d.value); };
colors.forEach(func);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments