patternjavascriptMinor
Trim() every string in a JavaScript object?
Viewed 0 times
trimjavascripteveryobjectstring
Problem
We have a database with fixed width columns. The API is really simple and returns everything the database does with little to no actual processing. This results in some unnecessarily long strings in the model. The model has child objects who have grandchildren and can get quite large at times.
I've written this function to clean up a model right after it's received from the API and right before the rest of the client does anything with it:
I know browsers don't really pay attention to consecutive spaces, but this is causing problems when we determine if a field has any valuable content. We can't simply check for a falsey from the string because being filled with spaces makes it just as true as it being filled with an address, phone numbers, or other meaningful data.
This seems to function properly for the few cases I've tried, but it's slow for large objects, especially in IE9 (the oldest browser we're supporting).
Performance is all I care about. It won't be running a thousand times a second, but it might get large, deep objects. Is there any way to speed this up?
I've written this function to clean up a model right after it's received from the API and right before the rest of the client does anything with it:
function DeepTrim(obj) {
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
if (typeof obj[prop] == 'string') {
obj[prop] = obj[prop].trim();
} else if (typeof obj[prop] == 'object') {
DeepTrim(obj[prop]);
}
}
}
}I know browsers don't really pay attention to consecutive spaces, but this is causing problems when we determine if a field has any valuable content. We can't simply check for a falsey from the string because being filled with spaces makes it just as true as it being filled with an address, phone numbers, or other meaningful data.
This seems to function properly for the few cases I've tried, but it's slow for large objects, especially in IE9 (the oldest browser we're supporting).
Performance is all I care about. It won't be running a thousand times a second, but it might get large, deep objects. Is there any way to speed this up?
Solution
I see a bug for a
Another less important? bug is you can't use the key
Only obvious optimization I see is to move the
I assume you're polyfilling trim for older browsers right?
null property case as typeof null == "object". You should check if it's not null or an object :)Another less important? bug is you can't use the key
hasOwnProeprty on any of your objects. You can get around this by using Object.prototype.hasOwnProperty.call(obj, key)Only obvious optimization I see is to move the
hasOwnProperty check after the typeof check. hasOwnProperty is a relatively expensive function so you will probably see some gains off that. I expect this should be even faster than doing Object.keys which is known to be faster than your for in has loop...I assume you're polyfilling trim for older browsers right?
function DeepTrim(obj) {
for (var prop in obj) {
var value = obj[prop], type = typeof value;
if (value != null && (type == "string" || type == "object") && obj.hasOwnProperty(prop)) {
if (type == "object") {
DeepTrim(obj[prop]);
} else {
obj[prop] = obj[prop].trim();
}
}
}
}Code Snippets
function DeepTrim(obj) {
for (var prop in obj) {
var value = obj[prop], type = typeof value;
if (value != null && (type == "string" || type == "object") && obj.hasOwnProperty(prop)) {
if (type == "object") {
DeepTrim(obj[prop]);
} else {
obj[prop] = obj[prop].trim();
}
}
}
}Context
StackExchange Code Review Q#59535, answer score: 6
Revisions (0)
No revisions yet.