G
Grant Wagner
I'm a bit confused by String() (typeof 'string') vs new String() (typeof
'object'). When you need to access a method or property of a -String-,
what type is JavaScript expecting (or rather, what should you provide),
a -String object- or a -string-?
Given the following benchmark:
var t = (new Date()).getTime();
for (var ii = 0; ii < 100000; ++ii) {
var x = (String('hi')).charAt(0); // typeof 'string'
}
document.write(((new Date()).getTime() - t) + '<br>');
var t = (new Date()).getTime();
for (var ii = 0; ii < 100000; ++ii) {
var x = (new String('hi')).charAt(0); // typeof 'object'
}
document.write(((new Date()).getTime() - t) + '<br>');
Internet Explorer is consistently faster when you apply a string method
to a -String()- than a -new String()-, yet most other browsers are
consistently only slightly slower applying a method to a -String()-.
This implies to me that JavaScript in most browsers needs to take
a -string- and wrap it in a -String object- before applying the method.
Anyway, the question isn't really about performance, it's more about
proper design. Which is "more correct"?
// avoid errors for non-string types
(String(unknownType)).replace(/something/, 'somethingElse')
(new String(unknownType)).replace(/something/, 'somethingElse')
Given that methods of String seem to return a -string- rather than
a -String object- and you can chain method calls together
(String(s)).replace(...).replace(...).replace(...) it just seems that
for "consistency" it's better to call your first String method on
a -string- rather than a -String object-.
Not to mention I have several functions which include code such as:
if ('string' == typeof s) {
if (/* some test on s */) return true;
}
return false;
and obviously these produce the wrong results when someone creates a
String object with new String() and passes it to my function. So I end
up refactoring the function to do something like:
if (null != s) {
if ('string' != typeof s) {
s = String(s); // s.toString() ?
}
if (/* some test on s */) return true;
}
return false;
It just all feels horribly cumbersome.
'object'). When you need to access a method or property of a -String-,
what type is JavaScript expecting (or rather, what should you provide),
a -String object- or a -string-?
Given the following benchmark:
var t = (new Date()).getTime();
for (var ii = 0; ii < 100000; ++ii) {
var x = (String('hi')).charAt(0); // typeof 'string'
}
document.write(((new Date()).getTime() - t) + '<br>');
var t = (new Date()).getTime();
for (var ii = 0; ii < 100000; ++ii) {
var x = (new String('hi')).charAt(0); // typeof 'object'
}
document.write(((new Date()).getTime() - t) + '<br>');
Internet Explorer is consistently faster when you apply a string method
to a -String()- than a -new String()-, yet most other browsers are
consistently only slightly slower applying a method to a -String()-.
This implies to me that JavaScript in most browsers needs to take
a -string- and wrap it in a -String object- before applying the method.
Anyway, the question isn't really about performance, it's more about
proper design. Which is "more correct"?
// avoid errors for non-string types
(String(unknownType)).replace(/something/, 'somethingElse')
(new String(unknownType)).replace(/something/, 'somethingElse')
Given that methods of String seem to return a -string- rather than
a -String object- and you can chain method calls together
(String(s)).replace(...).replace(...).replace(...) it just seems that
for "consistency" it's better to call your first String method on
a -string- rather than a -String object-.
Not to mention I have several functions which include code such as:
if ('string' == typeof s) {
if (/* some test on s */) return true;
}
return false;
and obviously these produce the wrong results when someone creates a
String object with new String() and passes it to my function. So I end
up refactoring the function to do something like:
if (null != s) {
if ('string' != typeof s) {
s = String(s); // s.toString() ?
}
if (/* some test on s */) return true;
}
return false;
It just all feels horribly cumbersome.