I need to iterate through two objects and check if their keys match. The trick is, I need to ensure the keys are both lower case. I've posted about it here.
This is done easily enough if I just loop through both objects and check for matches. But that means I could have at worst a runtime till the very last key in each object.
I put a counter on it and ended up with a high number of checks:
var i = 0;
for (key in objA) {
for (key2 in objB) {
i++; //ends up at 185 checks
if (key.toLowerCase() === key2.toLowerCase()) {
console.log('match');
I'm not sure what performance differences there are with if (k in..)
syntax since I can't iterate thru the inner loop.
for (key in objA) {
if (key in objB) {
//i = ?
What is the performance difference between these two?
Note: The lengths of the objects don't match. Basically, I'm passing in a smaller object with keys that I need to check against a larger object. For the purposes of my project, I need to check if any keys in the smaller object are IN the larger object.
I checked using date time difference between start and finish.
var start = (new Date).getTime();
//loop
var diff = (new Date).getTime() - start;
Loop 1: 4 seconds
Loop 2: 1 second
The issue is, I need some way to efficiently check two lower case keys from different sized objects using the more efficient if (k in obj)
check.