"VK" <schools_ring@yahoo.com> wrote in message
news:1117117396.397338.134160@g47g2000cwa.googlegr oups.com...[color=blue]
> You are a bit confused about primitive values and primitive value
> wrappers.[/color]
No I'm not. My question was basically: Since the underlying
implementation, when confronted with "string".charAt(0); converts this
to new String("string").charAt(0); is it better to do this yourself in
advance as a matter of design (without regard for performance)?
[color=blue]
> It is ALWAYS more performance/resource effective to use primitives
> rather than object wrappers.[/color]
No it isn't. As I've shown, in most Web browsers it is more performant
(by a neligible margin) to wrap a string in a String object yourself
before calling a String.prototype method on it.
var s = 'test string';
var t = (new Date()).getTime();
for (var ii = 0; ii < 100000; ++ii) {
var x = s.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(s)).charAt(0); // typeof 'object'
}
document.write(((new Date()).getTime() - t) + '<br>');
In Firefox 1.0.4, Mozilla 1.7.8 and Netscape 4.78 (reduced loop counts
from 100000 to 10000) the second test (where I wrap the string in a
String object before invoking charAt() on it) has a slight (very slight)
advantage.
In Mozilla 1.0.2 and Opera 6.05, 7.54u2 and 8.00 the first test (where I
let the underlying implementation wrap it in a String object) has a
slight (very slight) advantage.
In Internet Explorer 6.0.2900 the first test has a significant advantage
(around 60% faster). I'm pretty sure it has something to do with this:
<url:
http://blogs.msdn.com/ericlippert/ar...2003/11/6.aspx />
---
If "bar" is not an object then how is it possible to say
print("bar".toUpperCase());
? Well, actually, from the point of view of the specification, this is
just a syntactic sugar for
print((new String("bar")).toUpperCase());
Now, of course as an implementation detail we do not actually cons up a
new object every time you call a property on a value type! That would
be a performance nightmare. The runtime engine is smart enough to
realize that it has a value type and that it ought to pass it as the
"this" object to the appropriate method on String.prototype and
everything just kind of works out.
---
JScript doesn't actually "con up" a new String() everytime, the other
implementations either: a) do "con up" a new String() everytime; or b)
take longer figuring out that they don't have to.
Anyway, my question wasn't really about performance, but more about
proper design. Handle everything as a string, or as a String object, or
switch back and forth as necessary.
[color=blue]
> The only reason to use object wrappers Object(), Boolean(), Number()
> and String() would be to create customized objects with extra
> properties and methods.[/color]
Maybe I don't understand what you are saying, but I disagree with the
way I'm interpretting it. I don't need to wrap a string in a String
object to use custom methods/properties I've added to the String
prototype:
String.prototype.repeat = function(n) {
return (new Array(n + 1)).join(this);
}
alert("abc".repeat(5));
Maybe you meant:
function MyString(s) {
var value = String(s);
this.toString = function() {
return value;
}
}
MyString.prototype = new String();
var ms = new MyString("abc");
alert(ms);
But then I'm creating a MyString object, not a String object.
--
Grant Wagner <gwagner@agricoreunited.com>
comp.lang.javascript FAQ -
http://jibbering.com/faq