We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I saw the code in your PM. The performance hit was in your for loop. You were checking to see if one substring had the other substring's character twice (once with includes() and once with indexOf()). Both those functions will search for the given character in a string/array, but includes() will return true or false and indexOf() will return the index of the character or -1 if it doesn't contain the character. So, if you only use indexOf() and check if the return value is === -1 instead of searching through the string twice. Below is an edited version of the code you sent me and you will see what i mean:
Anagram
You are viewing a single comment's thread. Return to all comments →
I saw the code in your PM. The performance hit was in your for loop. You were checking to see if one substring had the other substring's character twice (once with includes() and once with indexOf()). Both those functions will search for the given character in a string/array, but includes() will return true or false and indexOf() will return the index of the character or -1 if it doesn't contain the character. So, if you only use indexOf() and check if the return value is === -1 instead of searching through the string twice. Below is an edited version of the code you sent me and you will see what i mean: