Skip to content

Instantly share code, notes, and snippets.

@dongyuwei
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dongyuwei/f91a420b1284a49b38b3 to your computer and use it in GitHub Desktop.
Save dongyuwei/f91a420b1284a49b38b3 to your computer and use it in GitHub Desktop.
test leveldb keys prefix search with a large dict.
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/
@dongyuwei
Copy link
Author

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.

@dongyuwei
Copy link
Author

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