Caching using Object.keys | const cache = new Map();
function processNestedObject(obj){
const objKey = JSON.stringify(obj);
if (cache.has(objKey)) {
return cache.get(objKey);
}
cache.set(objKey, obj);
return obj;
}
function getProcessedObject(obj) {
const objKey = JSON.stringify(obj);
if (cache.has(objKey)) {
return cache.get(objKey);
}
return processNestedObject(obj);
}
let i = 100000;
while (i) {
i--;
getProcessedObject({
"Campaign": {
"title": {},
"subtitle": {},
"description": {},
"highlightedDescription": {},
"voucherCode": {},
"voucherLabel": {},
"color": {},
"textColor": {},
"cta": {
"Linkable": {},
"CampaignBasicCta": {},
"CampaignLinkCta": {
"text": {},
"uri": {}
},
"CampaignReleaseCta": {},
"CampaignExpiryCta": {}
},
"curator": {
"Curator": {
"id": {}
}
},
"media": {
"Media": {
"uri": {}
},
"Image": {
"uri": {}
},
"VideoStream": {
"uri": {}
},
"Video": {
"uri": {}
},
"Embed": {
"uri": {}
},
"PDF": {
"uri": {}
},
"Linkable": {
"uri": {}
}
},
"brand": {
"Brand": {
"name": {},
"id": {}
}
}
}
}
);
}
| ready |
Caching using properties | const cache = new Map();
function generateCacheKey(obj) {
const keyString = generateKeyString(obj);
return keyString.hashCode();
}
function generateKeyString(obj) {
const keys = Object.keys(obj).sort();
let keyString = "";
for (const key of keys) {
const value = obj[key];
if (typeof value === 'object' && value !== null) {
keyString += `${key}${generateKeyString(value)}`;
} else {
keyString += `${key}:${value}`;
}
}
return keyString;
}
String.prototype.hashCode = function () {
let hash = 0;
if (this.length === 0) {
return hash;
}
for (let i = 0; i < this.length; i++) {
const char = this.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash = hash & hash;
}
return hash;
};
function processNestedObject(obj) {
const objKey = generateCacheKey(obj);
if (cache.has(objKey)) {
return cache.get(objKey);
}
cache.set(objKey, obj);
return obj;
}
function getProcessedObject(obj) {
const objKey = generateCacheKey(obj);
if (cache.has(objKey)) {
return cache.get(objKey);
}
return processNestedObject(obj);
}
let i = 100000;
while (i) {
i--;
getProcessedObject({
"Campaign": {
"title": {},
"subtitle": {},
"description": {},
"highlightedDescription": {},
"voucherCode": {},
"voucherLabel": {},
"color": {},
"textColor": {},
"cta": {
"Linkable": {},
"CampaignBasicCta": {},
"CampaignLinkCta": {
"text": {},
"uri": {}
},
"CampaignReleaseCta": {},
"CampaignExpiryCta": {}
},
"curator": {
"Curator": {
"id": {}
}
},
"media": {
"Media": {
"uri": {}
},
"Image": {
"uri": {}
},
"VideoStream": {
"uri": {}
},
"Video": {
"uri": {}
},
"Embed": {
"uri": {}
},
"PDF": {
"uri": {}
},
"Linkable": {
"uri": {}
}
},
"brand": {
"Brand": {
"name": {},
"id": {}
}
}
}
});
}
| ready |
Caching using Object key using enumeration | const cache = new Map();
function generateCacheKey(obj) {
const keyString = generateKeyString(obj);
return keyString.hashCode();
}
function generateKeyString(obj) {
const keys = Object.getOwnPropertyNames(obj);
let keyString = "";
for (const key of keys) {
const value = obj[key];
if (typeof value === 'object' && value !== null) {
keyString += `${key}${generateKeyString(value)}`;
} else {
keyString += `${key}:${value}`;
}
}
return keyString;
}
String.prototype.hashCode = function () {
let hash = 0;
if (this.length === 0) {
return hash;
}
for (let i = 0; i < this.length; i++) {
const char = this.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash = hash & hash;
}
return hash;
};
function processNestedObject(obj) {
const objKey = generateCacheKey(obj);
if (cache.has(objKey)) {
return cache.get(objKey);
}
cache.set(objKey, obj);
return obj;
}
function getProcessedObject(obj) {
const objKey = generateCacheKey(obj);
if (cache.has(objKey)) {
return cache.get(objKey);
}
return processNestedObject(obj);
}
let i = 100000;
while (i) {
i--;
getProcessedObject({
"Campaign": {
"title": {},
"subtitle": {},
"description": {},
"highlightedDescription": {},
"voucherCode": {},
"voucherLabel": {},
"color": {},
"textColor": {},
"cta": {
"Linkable": {},
"CampaignBasicCta": {},
"CampaignLinkCta": {
"text": {},
"uri": {}
},
"CampaignReleaseCta": {},
"CampaignExpiryCta": {}
},
"curator": {
"Curator": {
"id": {}
}
},
"media": {
"Media": {
"uri": {}
},
"Image": {
"uri": {}
},
"VideoStream": {
"uri": {}
},
"Video": {
"uri": {}
},
"Embed": {
"uri": {}
},
"PDF": {
"uri": {}
},
"Linkable": {
"uri": {}
}
},
"brand": {
"Brand": {
"name": {},
"id": {}
}
}
}
});
}
| ready |