Skip to content

Instantly share code, notes, and snippets.

@Alfrex92
Last active March 6, 2019 13:30
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 Alfrex92/e94b1c7d3cf575f658ef4c713b784cd1 to your computer and use it in GitHub Desktop.
Save Alfrex92/e94b1c7d3cf575f658ef4c713b784cd1 to your computer and use it in GitHub Desktop.
const numbers = [1, 2, 3, 4, 5]
console.time('Imperative')
function squared(numbers) {
let arr = []
for ( let i = 0; i < numbers.length; i++ ) {
arr.push( numbers[i] * numbers[i] )
}
return console.log(arr)
}
squared(numbers)
console.timeEnd('Imperative')
console.time('Declarative')
console.log( numbers.map( num => num * num ) )
console.timeEnd('Declarative')
/*
Imperative:22.512ms
Declarative:1.87ms
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment