473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

String vs new String

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.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #1
8 2765


Grant Wagner wrote:
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-? Anyway, the question isn't really about performance, it's more about
proper design. Which is "more correct"?


In my view you should always work with primitive string values so use
string literals where you want to create strings or use
String(expression) where you need to convert other values to primitive
string values. There is no need and no advantage in explictly creating
String object instances in your script, if a method needs to be applied
to a primitive string value the script engine will do all that is
necessary under the hood.
On the other hand if you would use String object instances you need to
be aware that you are dealing with objects where comparison is on object
identity so with

var s = 'Kibology';
new String(s) == new String(s)

the comparison yields false which is much different to the primitive
string value comparison.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
On 26/05/2005 14:53, Grant Wagner wrote:
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-?
It doesn't really matter as far as the language is concerned. When a
member expression (identifier.identifier, or identifier[expression]) is
evaluated, the internal ToObject function is called on the left-hand
operand. For the built-in types, boolean, string, and number, this is
the same as creating a new object via the type's constructor function. So,

var value = 10,
string;

string = value.toString(16);

is functionally equivalent to:

string = (new Number(10)).toString(16);

In principle, it's more efficient for you to create the object yourself
if you're going to call a method several times using the same value.
But, if the value changes between each invocation, you may as well let
the engine perform the conversion for you as the built-in objects are
immutable so a new object is needed every time anyway.
[...] 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;


I suppose it depends on what you're doing. Sometimes I write functions
that expect either an object or a string. The former is meant to be an
element reference, whereas the latter is the id of some element - a bit
of convenience for the caller. In that situation, if an object had a
toString value that represented the id, then I'd leave it to the caller
to call toString, or use String as a function.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
VK
You are a bit confused about primitive values and primitive value
wrappers.

var Str = "Something";
creates a primitive value of type "string" (so it's typeof string)

var oStr = new String("Something")
creates an object using String class as its constructor (so its typeof
"object" and oStr.constructor = function String() {[native code]} ).

In the latter case the value itself becomes just one of properties of
this object: you need to use oStr.toSource() to get it.

It is ALWAYS more performance/resource effective to use primitives
rather than object wrappers.

The only reason to use object wrappers Object(), Boolean(), Number()
and String() would be to create customized objects with extra
properties and methods.

Jul 23 '05 #4
VK
Damn! I felt the crowd will jump on this one! :-))

Jul 23 '05 #5
"Grant Wagner" <gw*****@agricoreunited.com> wrote in message
news:yx****************@news2.mts.net...
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-?


Thanks for the various replies, it confirms mostly what I already knew
(that it doesn't matter much). I mostly just wanted to hear peoples'
reasoning on one choice or another.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #6
"VK" <sc**********@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
You are a bit confused about primitive values and primitive value
wrappers.
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)?
It is ALWAYS more performance/resource effective to use primitives
rather than object wrappers.
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.
The only reason to use object wrappers Object(), Boolean(), Number()
and String() would be to create customized objects with extra
properties and methods.


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 <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #7
Grant Wagner wrote:
"VK" <sc**********@yahoo.com> wrote [...]:
You are a bit confused about primitive values and primitive value
wrappers.
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)?


No.
It is ALWAYS more performance/resource effective to use primitives
rather than object wrappers.


No it isn't.


Yes it is.
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>');
Loops: 100
Repeats: 10

Result:
--------

Expressions:

[1] "foo".charAt(0)
[2] new String("foo").charAt(0)

Evaluation Results (ms):

\n| 1 2 3 4 5 6 7 8 9 10 min max avg
e\|______________________________
1| 34 55 54 33 55 31 55 32 53 32 31 55 43.4
2| 57 57 35 59 56 36 89 37 57 55 35 89 53.8

User Agent:
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.8) Gecko/20050517
Firefox/1.0.4 (Debian package 1.0.4-2) Mnenhy/0.7.2.0

Although this is still a singular result, the second expression in the
average always took more time to be evaluated than the first one, in all
of the 10 tests of the 10 x 100 loops performed.
In Firefox 1.0.4, [...] (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.


I cannot second that.
PointedEars
Jul 23 '05 #8
On 02/06/2005 18:31, Thomas 'PointedEars' Lahn wrote:

[snip]
[1] "foo".charAt(0)
[2] new String("foo").charAt(0)


[snip]

If you're merely evaluating one-time object instances, then the latter
will always be slower due to the overhead involved in the NewExpression.
I implied that much in my post. However, if the same object instance can
be used multiple times, then it will be quicker as there is no need to
create an intermediary. Evaluating new String(...) outside the inner
test loop should demonstrate this. I see an improvement of about 62%.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
2
by: Badass Scotsman | last post by:
Hello, Using VB and ASP,NET I would like to be able to search a STRING for a smaller STRING within, based on the characters which appear before and after. For example: String1 = " That was...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
11
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove...
8
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.