Skip to content

Instantly share code, notes, and snippets.

@StrykerKent
Last active March 29, 2019 18:25
Show Gist options
  • Save StrykerKent/722a1871d734ae431e73090dc7ea270e to your computer and use it in GitHub Desktop.
Save StrykerKent/722a1871d734ae431e73090dc7ea270e to your computer and use it in GitHub Desktop.
Reviewing / Measuring / Timing O(n) performance using JavaScript, NFL Players, functions, for loops, and performance.now(). For use in browser, not node.js.
const players = [
'Tom Brady',
'Aaron Rodgers',
'Drew Brees',
'Ben Roethlisberger',
'Philip Rivers',
'Aqib Talib',
'Alex Smith',
'Larry Fitzgerald'
];
function getPlayer(player, array) {
let t0 = performance.now();
for (let i = 0; i < array.length; i++ ) {
if (array[i] === player) {
console.log('Found player', player);
}
}
let t1 = performance.now();
console.log(`t0= ${t0} t1 = ${t1}`);
console.log(`Finding player ${player} took ${t1-t0} milliseconds.`);
}
getPlayer('Philip Rivers', players);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment