Text-change-digest is a fast digest/hash optimized for detecting changes to text. This makes it well-suited for low-latency, interactive applications. Our goal is to get the text change detection comfortably under 60ms, so that 60fps can be achieved on systems where text changes are polled at hundreds if not thousands of points.
Our main method of providing fast change detection is to pick a hashing algorithm with sufficient characteristics, and to avoid calculating it until the very last stage in our change detection sequence. We picked 32-bit Fowler-Noll-Vo 1a as our hashing algorithm. Fowler-Noll-Vo gives pretty good diffusion, and is fast to compute.
Since calculating the hash is relatively expensive, we will push it as far back as possible. To do this, we add quick fields for detecting changes to text. We chose the first character, the last character, and the length. Most changes can be quickly detected by fluctuations in either of the three. This will allow us to avoid computing the hash in order to detect a change most of the time.
To get an estimate of its performance characteristics, we created a benchmark. We repeatedly called the update method with a new performance.now() string, recorded the gap time til the update callback, then averaged all the recorded timings.
Here are the results in microseconds:
Browser: Google Chrome 43.0.2357.124 (64-bit)
Operating System: Mac OS X 10.10.3
CPU: 2.4 GHz Intel Core i7
Update cycles / run : 100,000
- Avg Latency: 1.010 microseconds
- Max Latency: 1.129 microseconds
- Min Latency: 0.944 microseconds
You can use NPM to install text-change-digest. With NPM:
npm install text-change-digest
And then in your Node source, you would do:
var tcdigest = ;
You can use Bower to install text-change-digest. With Bower:
bower install text-change-digest
And then in your web page, you can load text-change-digest simply by pointing a script tag to the location of tcdigest.js
<!-- window.tcdigest should now point to the tcdigest function -->
Create a new digest.
var createDigest = ;var digest = ;
Returns true if this digest equals otherDigest.
var createDigest = ;var digest = ;digest // Returns true;
Updates this digest using newText. If a change is detected anytime in the process, then fxChange is called immediately. When fxChange is called, the digest will be in an indeterminate state. We advise against accessing or modifying digest state during this span.
var createDigest = ;var digest = ;digest
Corner Cases : Null & Undefined
While we anticipate most the time, text-change-digest will be called with well-defined pieces of text, sometimes it can be useful to extend its functions to non-string values. Most useful among these are null and undefined. What happens for example, when the constructor is called with null, or .update is called with undefined. Rest assured, these cases are taken care of. Anytime a text-change-digest is called with null or undefined instead of text, the null/undefined value will map to the empty string before undergoing any processing.
We thought this would make the most sense and preserve the behavior that developers would expect. Please let us know if otherwise.
The MIT License (MIT)
Copyright (c) 2015 Dickson Tam
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.