Last active
April 13, 2023 15:01
-
-
Save sasknot/f43f271eb81440d9e2500a9c3e34c36c to your computer and use it in GitHub Desktop.
JavaScript benchmark example
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
const arr = ['love', 'lovec', 'wlovec', 'asdlov123', 'abc123'] | |
const word = 'love' |
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
for (const curWord of arr) { | |
if (word.split('').every((letter) => curWord.includes(letter))) { | |
console.log('1', curWord) | |
} | |
} |
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
for (const curWord of arr) { | |
let yes = true | |
for (const letter of word) { | |
if (!curWord.includes(letter)) { | |
yes = false | |
break | |
} | |
} | |
if (yes) { | |
console.log('2', curWord) | |
} | |
} |
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
for (const curWord of arr) { | |
let i | |
for (i = 0; i < word.length; i++) { | |
if (!curWord.includes(word.at(i))) { | |
break | |
} | |
} | |
if (i === word.length) { | |
console.log('3', curWord) | |
} | |
} |
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
for (let j = 0; j < arr.length; j++) { | |
let i; | |
for (i = 0; i < word.length; i++) { | |
if (!arr[j].includes(word.at(i))) { | |
break | |
} | |
} | |
if (i === word.length) { | |
console.log('4', arr[j]) | |
} | |
} |
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
loop1: | |
for (const curWord of arr) { | |
for (const letter of word) { | |
if (!curWord.includes(letter)) { | |
break loop1 | |
} | |
} | |
console.log('5', curWord) | |
} |
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
loop1: | |
for (const curWord of arr) { | |
for (let i = 0; i < word.length; i++) { | |
if (!curWord.includes(word.at(i))) { | |
break loop1 | |
} | |
} | |
console.log('6', curWord) | |
} |
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
loop1: | |
for (let j = 0; j < arr.length; j++) { | |
for (let i = 0; i < word.length; i++) { | |
if (!arr[j].includes(word.at(i))) { | |
break loop1 | |
} | |
} | |
console.log('7', arr[j]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test cases results
Conclusion
The lesser number of variables or functions, more faster our code will be.
Relying in simple structures like basic for and if is more performant.