Coder Social home page Coder Social logo

rope.js's Introduction

Rope library

A rope library for fast string splicing. ~10x faster than JS strings at doing small 5-char splices in a 200k string.

It's not really a rope though, it's an array of strings, not a binary tree.

The Rope class implements all of the String prototype methods, so it can be used as a drop-in replacement for String. Read the performance notes before you do though.

Installation

npm install rope

What's this?

The rope data structure keeps the string in 5kB segments. The data structure has no index for fast access so access is ~O(n/5k).

Splicing uses one access to find the insertion point, then splices the string into the segment at the insertion point. Splicing potentially removes emptied segments and splits up the extended segment into 2.5k segments if needed.

Push pushes a string as a new segment to the rope.

toString joins the segments together and returns the resulting string.

Performance notes

The rope structure helps with inserting and deleting data in the middle of large strings. Most of the other operations are slowed down by having to manage the rope structure.

Specifically, if you're handling strings less than 5k in size, the rope is going to slow you down. The rope comes into its own when you're dealing with large strings. With 10k strings, 5-char splices are just ~1.5x faster on the rope. But with 1MB strings, the speedup is 340x.

The Rope class implements all of the String prototype methods. Some methods take advantage of the rope structure, some are slowed down by the rope and some are slow convenience methods that cast to string for their operations.

Fast methods: splice, push, concat

Should be mostly unaffected: slice, substr, substring, trim, trimLeft, trimRight

Slowed down: charAt, charCodeAt, indexOf, lastIndexOf, to(Locale)?(Upper|Lower)Case

Slow methods: localeCompare, match, replace, search, split

In-place methods: splice, push, trimInPlace, trimLeftInPlace, trimRightInPlace, to(Locale)?(Upper|Lower)CaseInPlace

Usage

var r = new Rope(['Hello,', 'or', ' no']);
r.push("!");
r.splice(6, 5, ' World');
r.toString();
// "Hello, World!"
r.slice(7).toString();
// "World!"

r.charAt(7);
// "W"
r.charCodeAt(7);
// 87

r.toUpperCase().toString();
// "HELLO, WORLD!"
r.toLowerCase().toString();
// "hello, world!"

new Rope("  foobar ").trim().toString();
// "foobar"
new Rope("  foobar ").trimLeft().toString();
// "foobar "
new Rope("  foobar ").trimRight().toString();
// "  foobar"


// Convenience methods (slow)

// Replace does internally new Rope(r.toString().replace(...))
r.replace(/[A-Z]/g, 'X').toString();
// "Xello, Xorld!"

// Ditto for match: r.toString().match(...)
r.match(/\S+/g);
// ["Hello,", "World!"]

Test

npm test

Run benchmark

The benchmark does 100k small edit operations on a ~100 kB string. It's intended to simulate a person typing into a document.

npm run-script benchmark

Possible performance improvements

  • Cache toString results if you have read-heavy parts in your application.
  • Speed up access (i.e. "get me the segment with the Nth character")
    • Maintain an index of segment positions, tree with subtree size as node value: log n to update, log n read
  • Regexp engine that operates on ropes

License: MIT

(c) Ilmari Heikkinen 2013

rope.js's People

Contributors

kig avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.