Skip to content

Instantly share code, notes, and snippets.

@cyberdude
Last active August 22, 2019 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyberdude/b634ffc6425562d9c0ff8c44e4807427 to your computer and use it in GitHub Desktop.
Save cyberdude/b634ffc6425562d9c0ff8c44e4807427 to your computer and use it in GitHub Desktop.
var object = [
{
name: "a",
expired: false
},
{
name: "b",
expired: true
},
{
name: "c",
expired: false
},
{
name: "d",
expired: true
}
];
const functionTwo = () => {
object
.filter(o => o.expired)
.forEach(() => {
1 + 1;
});
};
const functionOne = () => {
object.forEach(o => {
if (!o.expired) {
return;
}
1 + 1;
});
};
var iterations = 1000000;
console.time("Function #1");
for (var i = 0; i < iterations; i++) {
functionOne();
}
console.timeEnd("Function #1");
console.time("Function #2");
for (var i = 0; i < iterations; i++) {
functionTwo();
}
console.timeEnd("Function #2");
@PauliusLiekis
Copy link

It's also worth having in mind that filter will create a new array, so potentially it causes GC time consumption too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment