Reputation: 16666
I'm trying to convert a Ruby script I wrote into Node.js, but my zlib compression output is way to big (1580 bytes instead of the 62 ruby gives me). What I'm compressing is very uniform, 10,000 bytes of data which can either be 65 or 122.
Ruby:
rimg = []
(1..100).each do |i|
(1..100).each do |j|
rimg << ((j > 20 && j < 30) || (j > 70 && j < 80) ? 65 : 122)
end
end
zd = Zlib::Deflate.new(Zlib::DEFAULT_COMPRESSION, Zlib::MAX_WBITS)
ximg = zd.deflate(rimg.pack('c*'), Zlib::FINISH)
zd.close
puts "bytesize #{ximg.bytesize}"
Bytesize is 62 bytes. Here is my JS:
var rimg = new Buffer(100*100);
rimg.fill(0);
for (var i = 1; i <= 100; i++) {
for (var j = 1; j <= 100; j++) {
rimg.writeInt8((((j > 20 && j < 30) || (j > 70 && j < 80) ? 65 : 122)), i*j-1);
}
}
Zlib.deflate(rimg, { windowBits: 15 }, function(err, result) {
console.log('-------')
console.log(result.length);
});
This outputs 1580. Node version is 5.1.1. What is the difference?
The header bytes for both are "78 9C" so they should be using the same compression
Upvotes: 0
Views: 147
Reputation: 112482
It seems that you did not check your node.js buffer after your for
loops to see if it really was what you thought it was. Your offset is i*j-1
? Maybe you meant i*100 + j - 101
? Or better yet, to avoid needless computations, just set k = 0
before the for loops and make the offset k++
.
Upvotes: 1