Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any libraries for efficiently editing large strings in Javascript?

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?

like image 825
Joseph Avatar asked Nov 05 '22 00:11

Joseph


1 Answers

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

like image 111
Joseph Avatar answered Nov 11 '22 13:11

Joseph