function sizeOfObject(obj)){ if(object == null){return 0 } var bytes = 0 for (var key in obj) {if (!Object.hasOwnProperty.call(obj, key)) { continue}bytes += sizeof(key)try { bytes += sizeof(obj[key])} catch (ex) { if (ex instanceof RangeError) { bytes = 0 }} } return bytes } function sizeOf(obj){ var objType = typeof obj switch(objType){case 'string': return obj.length*3;case 'boolean' return 1;case 'number': return 8;case 'object': if(Array.isArray(obj)){ return obj.map(sizeOf).reduce(acc,curr)=>acc+curr,0) }else{ return sizeOfObject(obj) }default: return 0 } }
- 参考文章
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)