Skip to content
Advertisement

Why does this dynamic programming optimization actually make code slower?

This is from Leetcode problem: Concatenated Words. Below is a working solution. I added what I thought to be an optimization (see code comment), but it actually slows down the code. If I remove the wrapping if statement, it runs faster.

To me, the optimization helps avoid having to:

  1. call an expensive O(n) substring()
  2. check inside wordsSet
  3. making an unnecessary function call to checkConcatenation

Surely if (!badStartIndices.has(end + 1)) isn’t more expensive than all the above, right? Maybe it has something to do with Javascript JIT compilation? V8? Thoughts?

Use the following test input:

  // Notice how the second string ends with a 'b'!
  const words = [
  'a',
  'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab',
];
// Function in question.
var findAllConcatenatedWordsInADict = function (words) {
  console.time();
  // 1) put all words in a set
  const wordsSet = new Set(words);
  let badStartIndices;

  // 2) iterate words, recursively check if it's a valid word
  const concatenatedWords = [];
  function checkConcatenation(word, startIdx = 0, matches = 0) {
    if (badStartIndices.has(startIdx)) {
      return false;
    }

    if (startIdx === word.length && matches >= 2) {
      concatenatedWords.push(word);
      return true;
    }

    for (let end = startIdx; end < word.length; end++) {
      // I ADDED THE IF STATEMENT AS AN OPTIMIZATION. BUT CODE RUNS FASTER WITHOUT IT.
      // NOTE: Code is correct with & without if statement.
      if (!badStartIndices.has(end + 1)) { 
        const curWord = word.substring(startIdx, end + 1);
        if (wordsSet.has(curWord)) {
          if (checkConcatenation(word, end + 1, matches + 1)) {
            return true;
          }
        }
      }
    }

    badStartIndices.add(startIdx);
    return false;
  }

  for (const word of words) {
    // reset memo at beginning of each word
    badStartIndices = new Set();
    checkConcatenation(word);
  }

  console.timeEnd();
  return concatenatedWords;
};

Advertisement

Answer

Turns out this depends entirely on the input data, not on JavaScript or V8. (And as of writing this, I don’t know what data you used for benchmarking.)

With the example input from the Leetcode page you’ve linked, badStartIndices never does anything useful (both of the .has checks always return false); so it’s fairly obvious that doing this fruitless check twice is a little slower than doing it just once. In that case, the “dynamic programming” mechanism of the solution never kicks in, so the effective behavior degenerates to brute force, which is good enough because the input data is well-behaved. (In fact, deleting badStartIndices entirely would be even faster for such a test case.)

If I construct “evil” input data that actually leads to exponential combinatorial blow-up, i.e. where the badStartIndices.has(...) checks actually have something to do, then adding the early check does have a (small) performance benefit. (And without either of the checks, the computation would take “forever” for such inputs.)

So, taking a step back, this is one more example to illustrate that benchmarking is difficult; in particular, in order to get useful results, care must be taken to select relevant/realistic input data.
If the tests are too simple, developers are likely to not build optimizations that would help (a little or a lot) in high-load situations.
If the tests are too demanding, developers are likely to waste time on overly complicated code that ends up being slower than it could be for its target use case.
And if the code must handle any input with maximum performance, then as the developer you have the extra challenge of avoiding overhead for simple inputs while still scaling well to tough inputs…

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement