testing

Benchmark created on


Setup

queryWords = ["AMD", "dev"]

words = ["developers", "amd", "abc", "xyz", "AllegianceMD", "Egypt"]

Test runner

Ready to run.

Testing in
TestOps/sec
test_1
(function() {
    for (const currentQueryWord of queryWords) {
          let found = false;
          for (const word of words) {
            if (contains(word, currentQueryWord)) {
               found = true;
               break;
            }
          }
    
          if (!found) return false;
    }
})();
    
function contains(word, query) {

    // why not using word.includes(query) ?
    // because localeCompare function can be diacritic/case in/sensitive
    for (let start = 0; start + query.length <= word.length; ++start) {
        const substring = word.substring(start, start + query.length);
        // if (!substring.localeCompare(query, 'en', {
        // sensitivity: 'base',
        // usage: "search",
        // ignorePunctuation: true
        // })) {
        // return true;
        // }
        if(substring === query) return true;
    }
    return false;
}
ready
test_2
function contains(word, query) {
    // why not using word.includes(query) ?
    // because localeCompare function can be diacritic/case in/sensitive
    return word.includes(query);
  }
 
(function(){
      for (const currentQueryWord of queryWords) {
         let found = false;
         for (const word of words) {
         if (contains(word, currentQueryWord)) {
           found = true;
          break;
          }
        }

        if (!found) return false;
     }
 })();

ready
kmp
(function() {
    for (const currentQueryWord of queryWords) {
          let found = false;
          for (const word of words) {
            if (contains(word, currentQueryWord)) {
               found = true;
               break;
            }
          }
    
          if (!found) return false;
    }
})();
    
function contains(word, query) {

  // Create the prefix table
  const prefixTable = createPrefixTable(pattern);

  let j = 0; // Index of pattern
  let i = 0; // Index of text

  while (i < text.length) {

    if(!text[i].localeCompare(pattern[j], 'en', {
        sensitivity: 'base',
        usage: "search",
        ignorePunctuation: true
        })) {
        // Character matches, move to next character
      i++;
      j++;
        
      if (j === pattern.length) {
        // Pattern found, return the start index
        return i - j;
      }
    }

    // if (text[i] === pattern[j]) {
    //   // Character matches, move to next character
    //   i++;
    //   j++;
        
    //   if (j === pattern.length) {
    //     // Pattern found, return the start index
    //     return i - j;
    //   }
    // } 
    else if (j > 0) {
      // Character doesn't match, use prefix table to update the pattern index
      j = prefixTable[j - 1];
    } 
    
    else {
      // Character doesn't match and pattern index is 0, move to next character in text
      i++;
    }
  }

  // Pattern not found
  return -1;
}

function createPrefixTable(pattern) {
    const prefixTable = new Array(pattern.length).fill(0);
  
    let j = 0;
    for (let i = 1; i < pattern.length; i++) {
      while (j > 0 && pattern[i] !== pattern[j]) {
        j = prefixTable[j - 1];
      }
  
      if (pattern[i] === pattern[j]) {
        j++;
      }
  
      prefixTable[i] = j;
    }
  
    return prefixTable;
  }
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.