Last active
August 29, 2015 14:01
-
-
Save dongyuwei/f91a420b1284a49b38b3 to your computer and use it in GitHub Desktop.
test leveldb keys prefix search with a large dict.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var levelup = require('levelup') | |
// open a data store | |
var db = levelup('/tmp/foo.leveldb') | |
var list = []; | |
var start = new Date().getTime(); | |
var prefix = "ha"; | |
//cat /usr/share/dict/words | grep "^ha" | wc | |
db.createReadStream({ | |
start: prefix, // jump to first key with the prefix | |
end: prefix + "\xFF" // stop at the last key with the prefix | |
}) | |
.on('data', function(data) { | |
list.push(data.key); | |
// console.log(data) | |
}) | |
.on('error', function() { | |
console.log('error', arguments) | |
}) | |
.on('close', function() { | |
console.log('close', list, list.length, (new Date().getTime()) - start) | |
}); | |
//ref //http://dailyjs.com/2013/05/03/leveldb-and-node-2/ |
cat /usr/share/dict/words | grep "^ha" | wc
grep
is really fast ,too!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Before that, /tmp/foo.leveldb stored all words in Mac OSX's /usr/share/dict/words. The key and the value is the same word.
Leveldb keys prefix search is fast enough to do a word auto-suggestion.