Simple speed up

Part of my core algorithm for generating word searches involves cloning the existing state of a word search grid.

Today I tried to speed this up to see what effect it had.

What an effect it had.

The algorithm is now about five times faster. Not bad for a one line change.

Turns out JSON encoding and decoding in order to clone isn't too efficient. Not too surprising, but it got the job done. The new method uses a map and slice to achieve the same result, and does it much more efficiently.

JSON.parse JSON.stringify grid // Time taken: 24m10.106s  

is beaten hands-down by

_.map grid, (row) -> row.slice 0 // Time taken: 4m7.985s  

I've heard rumours that using slice 0 is faster than slice alone, but I haven't tried that yet. I'm also curious to see what I might be able to achieve by storing rows as strings or, in fact, storing the whole grid in a string.

Stay tuned!

[Image credit: Martin Heigan]