如何计算对象或javascript中压缩对象的实际大小(字节),而不是基于对象中字符的长度?
我尝试了object-sizeof npm库,但是它会根据字符长度返回大小。在下面的代码中,jsonCompressCheck是一个大的Json Object,我需要压缩它并且我想获取大小以便知道它的效率是
console.log("Actual Object Size: "+sizeof($scope.jsonCompressCheck));
var zlib = require('zlib');
var sizeof = require('object-sizeof');
zlib.deflate(JSON.stringify($scope.jsonCompressCheck),function(err, buffer) {
if (!err) {
console.log("USing Deflate Size of: ");
console.log("Byte Length: "+Buffer.byteLength(buffer));
console.log(sizeof(buffer));
$scope.compressed = buffer;
}
});
zlib.gzip(JSON.stringify($scope.jsonCompressCheck),function(err, buffer) {
if (!err) {
console.log("USing gzip: ");
console.log("Byte Length: "+Buffer.byteLength(buffer));
console.log(sizeof(buffer));
$scope.compressed = buffer;
}
});