snappyjs
Advanced tools
Comparing version 0.3.1 to 0.4.0
{ | ||
"name": "snappyjs", | ||
"main": "./dist/snappyjs.js", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"homepage": "https://github.com/zhipeng-jia/snappyjs", | ||
@@ -6,0 +6,0 @@ "authors": [ |
{ | ||
"name": "snappyjs", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "JavaScript implementation of Google's Snappy compression library", | ||
@@ -8,3 +8,3 @@ "repository": "zhipeng-jia/snappyjs", | ||
"scripts": { | ||
"test": "tap test.js" | ||
"test": "standard && tap test.js" | ||
}, | ||
@@ -22,2 +22,3 @@ "author": "Zhipeng Jia", | ||
"snappy": "~4.1.1", | ||
"standard": "~6.0.4", | ||
"tap": "~5.1.1", | ||
@@ -28,3 +29,14 @@ "uglifyify": "~3.0.1" | ||
"snappy" | ||
] | ||
], | ||
"standard": { | ||
"ignore": [ | ||
"/dist/snappyjs.js", | ||
"/dist/snappyjs.min.js" | ||
], | ||
"globals": [ | ||
"FileReader", | ||
"Benchmark", | ||
"SnappyJS" | ||
] | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# SnappyJS [![Travis CI status](https://travis-ci.org/zhipeng-jia/snappyjs.svg?branch=master)](https://travis-ci.org/zhipeng-jia/snappyjs) | ||
# SnappyJS [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) [![Travis CI status](https://travis-ci.org/zhipeng-jia/snappyjs.svg?branch=master)](https://travis-ci.org/zhipeng-jia/snappyjs) | ||
A pure JavaScript implementation of Google's [Snappy](https://github.com/google/snappy) compression library. | ||
@@ -54,23 +54,30 @@ | ||
~~~ | ||
Real text #1 (length 618425, byte length 1236850): | ||
node-snappy#compress x 170 ops/sec ±0.96% (74 runs sampled) | ||
snappyjs#compress x 54.34 ops/sec ±1.27% (67 runs sampled) | ||
node-snappy#uncompress x 461 ops/sec ±1.60% (73 runs sampled) | ||
snappyjs#uncompress x 164 ops/sec ±1.31% (70 runs sampled) | ||
Real text #1 (length 618425, byte length 618425), repeated 100 times: | ||
node-snappy#compress x 2.31 ops/sec ±1.47% (10 runs sampled) | ||
snappyjs#compress x 0.91 ops/sec ±0.92% (7 runs sampled) | ||
node-snappy#uncompress x 7.22 ops/sec ±4.07% (22 runs sampled) | ||
snappyjs#uncompress x 2.45 ops/sec ±1.53% (11 runs sampled) | ||
Real text #2 (length 3844590, byte length 7689180): | ||
node-snappy#compress x 42.77 ops/sec ±1.33% (54 runs sampled) | ||
snappyjs#compress x 17.78 ops/sec ±1.20% (46 runs sampled) | ||
node-snappy#uncompress x 101 ops/sec ±4.43% (61 runs sampled) | ||
snappyjs#uncompress x 40.93 ops/sec ±0.88% (52 runs sampled) | ||
Real text #2 (length 3844590, byte length 3844591), repeated 10 times: | ||
node-snappy#compress x 7.68 ops/sec ±2.78% (23 runs sampled) | ||
snappyjs#compress x 3.56 ops/sec ±1.44% (13 runs sampled) | ||
node-snappy#uncompress x 17.94 ops/sec ±4.71% (33 runs sampled) | ||
snappyjs#uncompress x 7.24 ops/sec ±2.57% (22 runs sampled) | ||
Random string (length 1000000, byte length 2000000): | ||
node-snappy#compress x 125 ops/sec ±1.76% (73 runs sampled) | ||
snappyjs#compress x 29.34 ops/sec ±2.41% (52 runs sampled) | ||
node-snappy#uncompress x 381 ops/sec ±3.33% (66 runs sampled) | ||
snappyjs#uncompress x 163 ops/sec ±1.40% (70 runs sampled) | ||
Random string (length 1000000, byte length 1500098), repeated 50 times: | ||
node-snappy#compress x 6.69 ops/sec ±5.23% (21 runs sampled) | ||
snappyjs#compress x 2.39 ops/sec ±2.54% (10 runs sampled) | ||
node-snappy#uncompress x 14.94 ops/sec ±6.90% (40 runs sampled) | ||
snappyjs#uncompress x 5.92 ops/sec ±4.28% (19 runs sampled) | ||
Random string (length 100, byte length 147), repeated 100000 times: | ||
node-snappy#compress x 4.17 ops/sec ±2.96% (15 runs sampled) | ||
snappyjs#compress x 5.45 ops/sec ±1.51% (18 runs sampled) | ||
node-snappy#uncompress x 4.39 ops/sec ±3.83% (15 runs sampled) | ||
snappyjs#uncompress x 14.01 ops/sec ±2.06% (38 runs sampled) | ||
~~~ | ||
From the result, we see that SnappyJS has 35%~40% performance of native implementation on uncompression, | ||
and 25%~35% performance on compression. | ||
From the result, we see that SnappyJS has 35%~45% performance of native implementation. | ||
If input size is small, SnappyJS may have better performance than `node-snappy`. | ||
It is because calling native function in JS is much more expensive than calling JS function. | ||
@@ -77,0 +84,0 @@ ## License |
@@ -28,10 +28,7 @@ // The MIT License (MIT) | ||
var HASH_TABLE_BITS = 14 | ||
var HASH_TABLE_SIZE = 1 << HASH_TABLE_BITS | ||
var MAX_HASH_TABLE_BITS = 14 | ||
var global_hash_tables = new Array(MAX_HASH_TABLE_BITS + 1) | ||
var HASH_FUNC_SHIFT = 32 - HASH_TABLE_BITS | ||
function hashFunc (key) { | ||
var h = key * 0x1e35a7bd | ||
return h >>> HASH_FUNC_SHIFT | ||
function hashFunc (key, hash_func_shift) { | ||
return (key * 0x1e35a7bd) >>> hash_func_shift | ||
} | ||
@@ -100,3 +97,15 @@ | ||
function compressFragment (input, ip, input_size, output, op, hash_table) { | ||
function compressFragment (input, ip, input_size, output, op) { | ||
var hash_table_bits = 1 | ||
while ((1 << hash_table_bits) <= input_size && | ||
hash_table_bits <= MAX_HASH_TABLE_BITS) { | ||
hash_table_bits += 1 | ||
} | ||
hash_table_bits -= 1 | ||
var hash_func_shift = 32 - hash_table_bits | ||
if (typeof global_hash_tables[hash_table_bits] === 'undefined') { | ||
global_hash_tables[hash_table_bits] = new Uint16Array(1 << hash_table_bits) | ||
} | ||
var hash_table = global_hash_tables[hash_table_bits] | ||
var i | ||
@@ -124,3 +133,3 @@ for (i = 0; i < hash_table.length; i++) { | ||
ip += 1 | ||
next_hash = hashFunc(load32(input, ip)) | ||
next_hash = hashFunc(load32(input, ip), hash_func_shift) | ||
@@ -140,3 +149,3 @@ while (flag) { | ||
} | ||
next_hash = hashFunc(load32(input, next_ip)) | ||
next_hash = hashFunc(load32(input, next_ip), hash_func_shift) | ||
candidate = base_ip + hash_table[hash] | ||
@@ -167,5 +176,5 @@ hash_table[hash] = ip - base_ip | ||
} | ||
prev_hash = hashFunc(load32(input, ip - 1)) | ||
prev_hash = hashFunc(load32(input, ip - 1), hash_func_shift) | ||
hash_table[prev_hash] = ip - 1 - base_ip | ||
cur_hash = hashFunc(load32(input, ip)) | ||
cur_hash = hashFunc(load32(input, ip), hash_func_shift) | ||
candidate = base_ip + hash_table[cur_hash] | ||
@@ -180,3 +189,3 @@ hash_table[cur_hash] = ip - base_ip | ||
ip += 1 | ||
next_hash = hashFunc(load32(input, ip)) | ||
next_hash = hashFunc(load32(input, ip), hash_func_shift) | ||
} | ||
@@ -206,3 +215,2 @@ } | ||
this.array = uncompressed | ||
this.hash_table = new Uint16Array(HASH_TABLE_SIZE) | ||
} | ||
@@ -221,3 +229,2 @@ | ||
var hash_table = this.hash_table | ||
var fragment_size | ||
@@ -228,3 +235,3 @@ | ||
fragment_size = Math.min(length - pos, BLOCK_SIZE) | ||
out_pos = compressFragment(array, pos, fragment_size, out_buffer, out_pos, hash_table) | ||
out_pos = compressFragment(array, pos, fragment_size, out_buffer, out_pos) | ||
pos += fragment_size | ||
@@ -231,0 +238,0 @@ } |
Sorry, the diff of this file is not supported yet
20078
450
86
10