I need to efficiently edit large text documents (eg, source code files) in javascript.
insertAtPosition(n, str)
and deleteAtPosition(n, length)
must be fast.
A naive string implementation is slow because each operation requires copying the contents of the document to a new string.
There are a few efficient ways to do this. I could use an array of lines (Ace aka Bespin does this), but this would be slow when there are super long lines or many short lines. A better implementation would use skip lists or some other clever data structure.
But, I would expect somebody to have implemented such a thing already.
Are there any libraries which already do this? I can't seem to find anything useful with Google - is there a common name for this algorithm problem?
I don't think there are any other libraries to do this, so I implemented it myself using skip lists. It might be faster to use a tree data structure, but skip lists are easy to implement and the closure compiled javascript is only 2.5 KB.
If there's anyone else with the same problem, enjoy:
https://github.com/josephg/jumprope
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With