Approximation calculation in a target memory occupancy js

RAM

Before long, I wanted to see how much memory is occupied by an object in JS, until recently due to the use of online services need to calculate the size of the end of the transmission from the data, so the demand is particularly strong.

Preliminaries

Js now we are using the high-level language, it is in memory to create a layer of abstraction on top of the details, we currently exposed to, take up memory is variable, the variable is fixed, the language specification statement , but due to the realization of the interpreter, a possible types of variables occupy memory, will be slightly regulate more, here we can ignore the first, so that we can follow the variable memory size specification defined a variable to calculate the amount of memory

js the type

undefined

null

boolean

number

string

object (to distinguish between normal arrays and objects)

npm - object-sizeof

The data type size official definition, there is a npm package traverse the object, and then successively calculated for each attribute memory occupied by summation, the approximate size of the object can be obtained

npm install object-sizeof
  var sizeof = require('object-sizeof')
  
  // 2B per character, 6 chars total => 12B
  console.log(sizeof({abc: 'def'}))
  
  // 8B for Number => 8B
  console.log(sizeof(12345))
  
  var param = { 
    'a': 1, 
    'b': 2, 
    'c': {
      'd': 4
    }
  }
  // 4 one two-bytes char strings and 3 eighth-bytes numbers => 32B
  console.log(sizeof(param))
 

First mounting object-sizeof, and then introduced into the approximate size of the object can be obtained

object-sizeof defects

Curious, I looked at the source of this package

function sizeOfObject (object) {
  if (object == null) {
    return 0
  }

  var bytes = 0
  for (var key in object) {
    if (!Object.hasOwnProperty.call(object, key)) {
      continue
    }

    bytes += sizeof(key)
    try {
      bytes += sizeof(object[key])
    } catch (ex) {
      if (ex instanceof RangeError) {
        // circular reference detected, final result might be incorrect
        // let's be nice and not throw an exception
        bytes = 0
      }
    }
  }

  return bytes
}
function sizeof (object) {
  if (Buffer.isBuffer(object)) {
    return object.length
  }

  var objectType = typeof (object)
  switch (objectType) {
    case 'string':
      return object.length * ECMA_SIZES.STRING
    case 'boolean': 
      return ECMA_SIZES.BOOLEAN // 4
    case 'number':
      return ECMA_SIZES.NUMBER //8
    case 'object':
      if (Array.isArray(object)) {
        return object.map(sizeof).reduce((acc, curr) => acc + curr, 0)
      } else {
        return sizeOfObject(object)
      }
    default:
      return 0
  }
}

First here, null and undefined memory is marked as 0, this is not true, then the string type, is also a character, Chinese and English occupied bytes are not the same, did not calculate the size of the object, but also Can use

Guess you like

Origin www.cnblogs.com/sefaultment/p/11518625.html