jQuery And Javascript TutorialsTechnical NewsWeb Tutorials

Calculate array size in Kilobyte, Megabyte and Gigabyte in JavaScript

In this web guide, I will show you how you can calculate array size in bytes, Kilobyte, Megabyte and Gigabyte using JavaScript. We all know array is a collection of objects and we use it for storing values. However, there are many conditions when you need to calculate size of array’s before storing it to database. If you have set limit in size of database then you need to calculate array size before storing it.

Steps to Calculate Array Size Using JavaScript

Create a new .html file in any location you want then open it in editor. Now copy and paste below code and then run it any browser

<script>

//Creating Array
var myArray = {
	'0' : "First Object",
	'1' : "Second Object",
	'2' : "Third Object",
	'3' : "Fourth Object",
	'4' : "Fifth Object",
}; 

//Settings Default Size to 0
var bytes = 0;
 
//Caclulate each object size using Javascript 
function getObjectSize(obj)
{
	
    if(obj !== null && obj !== undefined) {
		var objClass = Object.prototype.toString.call(obj).slice(8, -1);
	 
		if(objClass === 'Object' || objClass === 'Array') {
		  for(var key in obj) {
			 if(!obj.hasOwnProperty(key)) continue;
				getObjectSize(obj[key]);
		  }
		} else {
			 bytes += obj.toString().length * 2;
			 
		}
	}
	
	if(bytes < 1024) return bytes + " bytes";
        else if(bytes < 1048576) return(bytes / 1024).toFixed(3) + " KB";
        else if(bytes < 1073741824) return(bytes / 1048576).toFixed(3) + " MB";
        else return(bytes / 1073741824).toFixed(3) + " GB";
} 
 
//Alert Results 
alert(getObjectSize(myArray));
</script>

When you run this code you will see a  alert showing size of array 148 as shown below

148 bytes is the size of above array.

Briefing About Code

First we have create a variable myArray then in we define an array of 5 objects in variable as shown below

var myArray = {
	'0' : "First ObjectFirst Object",
	'1' : "Second Object",
	'2' : "Third Object",
	'3' : "Fourth Object",
	'4' : "Fifth Object",
};

Once creating an array then create a variable for setting default bytes as shown below

//Settings Default Size to 0
var bytes = 0;

After that we create a function with name getObjectSize then we pass array with parameter obj as shown below

function getObjectSize(obj)
{
	
    if(obj !== null && obj !== undefined) {
		var objClass = Object.prototype.toString.call(obj).slice(8, -1);
	 
		if(objClass === 'Object' || objClass === 'Array') {
		  for(var key in obj) {
			 if(!obj.hasOwnProperty(key)) continue;
				getObjectSize(obj[key]);
		  }
		} else {
			 bytes += obj.toString().length * 2;
			 
		}
	}
	
	if(bytes < 1024) return bytes + " bytes";
        else if(bytes < 1048576) return(bytes / 1024).toFixed(3) + " KB";
        else if(bytes < 1073741824) return(bytes / 1048576).toFixed(3) + " MB";
        else return(bytes / 1073741824).toFixed(3) + " GB";
}

In above function first we will use if condition for checking parameter is not null or undefined then we will check if parameter is a object or an array. If parameter is an object or array then we break it using for loop and calling function again for calculating all object sizes as shown above. If parameter is a string then function will get string length using JavaScript length function and then add into previous object sizes.

Once the bytes are calculated then use if else condition again for showing results in Megabyte, Kilobyte and Gigabyte.

 

 

 

 

Anil Mehra

I am Anil Mehra, a passionate, workaholic and a Full Stack Tech Savvy Programmer with true north towards growth. I have worked on 256 live projects in MNC. I am expertise in the field of Programming, Server Management, SEO, Blogging and SMO...

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button

Adblock Detected

Please turn off Ad blocker to view website