Answer by Steve Cooper for How to efficiently count the number of...
Here are some performance tests for three methods; https://jsperf.com/get-the-number-of-keys-in-an-object Object.keys().length 20,735 operations per second Very simple and compatible. Runs fast but...
View ArticleAnswer by Robert Sinclair for How to efficiently count the number of...
OP didn't specify if the object is a nodeList, if it is then you can just use length method on it directly. Example: buttons = document.querySelectorAll('[id=button)) { console.log('Found ' +...
View ArticleAnswer by Taquatech for How to efficiently count the number of...
I try to make it available to all object like this: Object.defineProperty(Object.prototype, "length", { get() { if (!Object.keys) { Object.keys = function (obj) { var keys = [],k; for (k in obj) { if...
View ArticleAnswer by Fayaz for How to efficiently count the number of keys/properties of...
You can use: Object.keys(objectName).length; and Object.values(objectName).length;
View ArticleAnswer by Flavien Volken for How to efficiently count the number of...
as answered above: Object.keys(obj).length But: as we have now a real Map class in ES6, I would suggest to use it instead of using the properties of an object. const map = new Map(); map.set("key",...
View ArticleAnswer by lepe for How to efficiently count the number of keys/properties of...
From: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty Object.defineProperty(obj, prop, descriptor) You can either add it to all your objects:...
View ArticleAnswer by Belldandu for How to efficiently count the number of...
To iterate on Avi Flax answer Object.keys(obj).length is correct for an object that doesnt have functions tied to it example: obj = {"lol": "what", owo: "pfft"}; Object.keys(obj).length; // should be 2...
View ArticleAnswer by Luc125 for How to efficiently count the number of keys/properties...
The standard Object implementation (ES5.1 Object Internal Properties and Methods) does not require an Object to track its number of keys/properties, so there should be no standard way to determine the...
View ArticleAnswer by BenderTheOffender for How to efficiently count the number of...
As stated by Avi Flax https://stackoverflow.com/a/4889658/1047014 Object.keys(obj).length will do the trick for all enumerable properties on your object but to also include the non-enumerable...
View ArticleAnswer by codejoecode for How to efficiently count the number of...
If jQuery above does not work, then try $(Object.Item).length
View ArticleAnswer by Mark Rhodes for How to efficiently count the number of...
For those that have Ext JS 4 in their project you can do: Ext.Object.getSize(myobj); The advantage of this is that it'll work on all Ext compatible browsers (IE6-IE8 included), however, I believe the...
View ArticleAnswer by hakunin for How to efficiently count the number of keys/properties...
For those who have Underscore.js included in their project you can do: _({a:'', b:''}).size() // => 2 or functional style: _.size({a:'', b:''}) // => 2
View ArticleAnswer by Click Upvote for How to efficiently count the number of...
How I've solved this problem is to build my own implementation of a basic list which keeps a record of how many items are stored in the object. Its very simple. Something like this: function...
View ArticleAnswer by studgeek for How to efficiently count the number of keys/properties...
If you are using Underscore.js you can use _.size (thanks @douwe): _.size(obj) Alternatively you can also use _.keys which might be clearer for some: _.keys(obj).length I highly recommend Underscore,...
View ArticleAnswer by Renaat De Muynck for How to efficiently count the number of...
You could use this code: if (!Object.keys) { Object.keys = function (obj) { var keys = [], k; for (k in obj) { if (Object.prototype.hasOwnProperty.call(obj, k)) { keys.push(k); } } return keys; }; }...
View ArticleAnswer by Avi Flax for How to efficiently count the number of keys/properties...
To do this in any ES5-compatible environment, such as Node, Chrome, IE 9+, Firefox 4+, or Safari 5+: Object.keys(obj).length Browser compatibility Object.keys documentation (includes a method you can...
View ArticleAnswer by Luke Bennett for How to efficiently count the number of...
I'm not aware of any way to do this, however to keep the iterations to a minimum, you could try checking for the existance of __count__ and if it doesn't exist (ie not Firefox) then you could iterate...
View ArticleAnswer by Confusion for How to efficiently count the number of...
If you are actually running into a performance problem I would suggest wrapping the calls that add/remove properties to/from the object with a function that also increments/decrements an appropriately...
View ArticleHow to efficiently count the number of keys/properties of an object in...
What's the fastest way to count the number of keys/properties of an object? It it possible to do this without iterating over the object? i.e. without doing var count = 0; for (k in myobj) if...
View ArticleAnswer by dazzafact for How to efficiently count the number of...
this works for both, Arrays and Objects//count objectsfunction count(obj){ return Object.keys(obj).length }//count everything else also (+String length)function count(obj){ if (typeof (obj) ===...
View ArticleAnswer by perona chan for How to efficiently count the number of...
Testconst person = { firstName: 'John', lastName: 'Doe'};console.log( Object.keys(person).length, Object.values(person).length, Object.entries(person).length)
View Article