Last active
August 22, 2019 15:53
-
-
Save cyberdude/b634ffc6425562d9c0ff8c44e4807427 to your computer and use it in GitHub Desktop.
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
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"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's also worth having in mind that filter will create a new array, so potentially it causes GC time consumption too.