mbest / knockout

My enhancements to Knockout
35 stars 4 forks source link

optimizing compareArrays #19

Closed mbest closed 11 years ago

mbest commented 12 years ago

Part 1:

  1. For any two arrays, only run comparison once, with a calculated value of the comparison range (originally called max allowed distance). This value is equal to the difference in length of the two arrays. This greatly improves the performance when comparing arrays where the difference in length is greater than 1.
  2. When calculating the edit distance matrix, start with 1 instead of 0. Since zero is no longer a valid value, we can change code like a[x][y] === undefined ? maxValue : a[x][y] to a[x][y] || maxValue, which is more efficient.
  3. When calculating the edit script from the distance matrix, simplify the comparisons by recognizing that additions and deletions are represented by a west or north movement in the matrix to a value one less than the current value.
  4. After the edit script is calculated, compare the added and deleted items to find matches and mark them as moves. This allows the code that uses compareArrays to move items instead of deleting and re-adding them.
mbest commented 12 years ago

Part 2:

  1. Levenshtein distance function is always given smaller array on the left. Previously, if the larger array was on the left, it would mean more outer loops (overall more computation) and result in more comparisons because the compare range was applied more times. With this change, the comparison metrics will be the same regardless of the direction (small to big vs big to small).
  2. Comparison count is smaller while still giving a valid result for all comparisons. Previously, the compare range was effectively double the difference in length of the two arrays (although it would get clipped at the edges). Now, it's the difference in length plus 1. For comparison between arrays that have a large difference in length, this reduces the number of comparisons significantly.
mbest commented 11 years ago

This is part of Knockout 2.2.0