Skip to content

Instantly share code, notes, and snippets.

@EduApps-CDG
Last active December 15, 2019 13:49
Show Gist options
  • Save EduApps-CDG/98b4d85f0f3884fd29edadc2178f3e4e to your computer and use it in GitHub Desktop.
Save EduApps-CDG/98b4d85f0f3884fd29edadc2178f3e4e to your computer and use it in GitHub Desktop.
/**
* Copyright 2019 EduApps (Eduardo P. Gomez)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var ObjectTools = {
help: function() {
console.log("ObjectTools.js V1.0.0 by EduApps\n<> - needed variable.\n[] - not needed variable.\n\ncommands:\n help - show this help message()\n" +
" Length - returns the number of child Objects inside the Object root(<object>)\n" +
" getRAM - return the size (in bytes) occupied by the Object in RAM(<>object)");
},
Length: function(object) {
var length = 0;
for (var key in object) {
if (object.hasOwnProperty(key)) {
++length;
}
}
return length;
},
getRAM: function(object) {
var objectList = [];
var stack = [object];
var bytes = 0;
while (stack.length) {
var value = stack.pop();
if (typeof value === 'boolean') {
bytes += 4;
} else if (typeof value === 'string') {
bytes += value.length * 2;
} else if (typeof value === 'number') {
bytes += 8;
} else if (typeof value === 'object' && objectList.indexOf( value ) === -1) {
objectList.push(value);
for( var i in value ) {
stack.push( value[ i ] );
}
}
}
return bytes;
}
}
/**
* Copyright 2019 EduApps (Eduardo P. Gomez)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var ObjectTools={help:function(){console.log("ObjectTools.js V1.0.0 by EduApps\n<> - needed variable.\n[] - not needed variable.\n\ncommands:\n help - show this help message()\n"+" Length - returns the number of child Objects inside the Object root(<object>)\n"+" getRAM - return the size (in bytes) occupied by the Object in RAM(<>object)")},Length:function(object){var length=0;for(var key in object){if(object.hasOwnProperty(key)){++length}}return length},getRAM:function(object){var objectList=[];var stack=[object];var bytes=0;while(stack.length){var value=stack.pop();if(typeof value==='boolean'){bytes+=4}else if(typeof value==='string'){bytes+=value.length*2}else if(typeof value==='number'){bytes+=8}else if(typeof value==='object'&&objectList.indexOf(value)===-1){objectList.push(value);for(var i in value){stack.push(value[i])}}}return bytes}}
@EduApps-CDG
Copy link
Author

EduApps-CDG commented Dec 15, 2019

What it can do:

  • get the RAM size of a Object
  • get the number of child Objects

How to do:

see a list of command by typing (on javascript console):

> ObjectTools.help();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment