-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
V2 - uses precise calculation for node.js, and separate implementatio…
…n for the browser. (#70) * Implemented v2.0, using Buffer.from(objectToString) method to convert the string representation of the object to a buffer and then it uses the byteLength property to obtain the size of the buffer in bytes * Updated npm badge * Got rid of v1 * Preserved TS type * Simplified and modularised the code * Added code coverage * Added badges * Added coding standards section
- Loading branch information
Showing
10 changed files
with
6,804 additions
and
1,029 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,6 @@ npm-debug.log | |
node_modules | ||
.idea | ||
.vscode | ||
|
||
.nyc_output | ||
coverage.lcov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ node_js: | |
- v12 | ||
- v13 | ||
- v14 | ||
- v16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,6 @@ | |
module.exports = { | ||
STRING: 2, | ||
BOOLEAN: 4, | ||
BYTES: 4, | ||
NUMBER: 8 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* Calculates the approximate number of bytes that the provided object holds. | ||
* @param object | ||
*/ | ||
export default function sizeof<T>(object: T): number; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright 2023 ChatGPT Jan 9 Version | ||
/* eslint-disable new-cap */ // to fix new Buffer.from | ||
'use strict' | ||
const ECMA_SIZES = require('./byte_size') | ||
|
||
/** | ||
* Size in bytes in a Node.js environment | ||
* @param {*} obj | ||
* @returns size in bytes, or -1 if JSON.stringify threw an exception | ||
*/ | ||
function objectSizeNode (obj) { | ||
let totalSize = 0 | ||
const errorIndication = -1 | ||
try { | ||
const objectToString = JSON.stringify(obj) | ||
const buffer = new Buffer.from(objectToString) | ||
totalSize = buffer.byteLength | ||
} catch (ex) { | ||
console.error('Error detected, return ' + errorIndication, ex) | ||
return errorIndication | ||
} | ||
return totalSize | ||
} | ||
|
||
/** | ||
* Size in bytes in a browser environment | ||
* @param {*} obj | ||
* @returns size in bytes | ||
*/ | ||
function objectSizeBrowser (obj) { | ||
const objectList = [] | ||
const stack = [obj] | ||
let bytes = 0 | ||
|
||
while (stack.length) { | ||
const value = stack.pop() | ||
|
||
if (typeof value === 'boolean') { | ||
bytes += ECMA_SIZES.BYTES | ||
} else if (typeof value === 'string') { | ||
bytes += value.length * ECMA_SIZES.STRING | ||
} else if (typeof value === 'number') { | ||
bytes += ECMA_SIZES.NUMBER | ||
} else if (typeof value === 'symbol') { | ||
const isGlobalSymbol = Symbol.keyFor && Symbol.keyFor(obj) | ||
if (isGlobalSymbol) { | ||
bytes += Symbol.keyFor(obj).length * ECMA_SIZES.STRING | ||
} else { | ||
bytes += (obj.toString().length - 8) * ECMA_SIZES.STRING | ||
} | ||
} else if (typeof value === 'object' && objectList.indexOf(value) === -1) { | ||
objectList.push(value) | ||
|
||
for (const i in value) { | ||
stack.push(value[i]) | ||
} | ||
} | ||
} | ||
return bytes | ||
} | ||
|
||
/** | ||
* Are we running in a Node.js environment | ||
* @returns boolean | ||
*/ | ||
function isNodeEnvironment () { | ||
if ( | ||
typeof process !== 'undefined' && | ||
process.versions && | ||
process.versions.node | ||
) { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
module.exports = function (obj) { | ||
let totalSize = 0 | ||
|
||
if (obj !== null && typeof obj === 'object' && isNodeEnvironment()) { | ||
totalSize = objectSizeNode(obj) | ||
} else { | ||
totalSize = objectSizeBrowser(obj) | ||
} | ||
|
||
return totalSize | ||
} |
Oops, something went wrong.
c8675b6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi. Not sure if I understand correctly, but is the node.js path measuring the byte length of the stringified object? What is the relationship between this value and actual memory usage of an object in the js engine - seems like a generous upper bound to me. What about deep objects with circular references? Seems to me ChatGPT needs to do some more deep thinking on this ;)
c8675b6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with the comment.
1.6.3 became such a mess, tests did not even pass, so it´s time to rebuild it, having better test structure, and code coverage.