[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Style: new String( "abc"

ram

6/2/2015 5:08:00 PM

IIRC, I read somewhere that one should prefer to use
primitives such as "abc"; one should not create objects
via »new String ( "abc )« (or, »new Number( 7 )«).

I am not sure why »new String( "abc" )« should be bad style.

For example, I consider the following piece of code:

{ "abc".charAt( 0 );
"abc".charAt( 1 );
"abc".charAt( 2 ); }

(Of course, the example with »"abc".charAt( 0 );« is nonsense.)

. Above, AFAIK, the primitve "abc" was implicitly converted
into an object /three times/. If one would have written:

{ let abc = new String( "abc" );
abc.charAt( 0 );
abc.charAt( 1 );
abc.charAt( 2 ); }

only one object would have been created. Wouldn't this be more
efficient? So, why could it be bad style?

21 Answers

Andreas Bergmaier

6/2/2015 7:26:00 PM

0

Stefan Ram schrieb:

> the primitve "abc" was implicitly converted
> into an object /three times/.

In the old times, maybe. Current compilers don't convert anything to an
object any more here, they just call the native
`String.prototype.charAt` method directly on the primitive value.

> Wouldn't this be more
> efficient? So, why could it be bad style?

Even efficient code can be bad style :-) (think about goto for an
extreme example).
The wrapper creation is just unnecessary and overly verbose. With very
little gain.

Bergi

Thomas 'PointedEars' Lahn

6/2/2015 8:28:00 PM

0

Andreas Bergmaier wrote:

> Stefan Ram schrieb:
>> the primitve "abc" was implicitly converted
>> into an object /three times/.
>
> In the old times, maybe. Current compilers don't convert anything to an
> object any more here, they just call the native
> `String.prototype.charAt` method directly on the primitive value.

Cite evidence.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Thomas 'PointedEars' Lahn

6/2/2015 8:39:00 PM

0

Stefan Ram wrote:

> IIRC, I read somewhere that one should prefer to use
> primitives such as "abc"; one should not create objects
> via »new String ( "abc )« (or, »new Number( 7 )«).
>
> I am not sure why »new String( "abc" )« should be bad style.

Certainly there is nothing wrong per se in having objects that are
â??instanceof Stringâ?, see e.g.
<http://PointedEars.de/wsvn/JSX/string/unic....

But consider, for example, what

var b = new Boolean(false);
var b = new Number(0);
var s = new String("");

would do in

if (b && n && s)

when used instead of

var b = false;
var n = 0;
var s = "";

> For example, I consider the following piece of code:
>
> { "abc".charAt( 0 );
> "abc".charAt( 1 );
> "abc".charAt( 2 ); }
>
> (Of course, the example with »"abc".charAt( 0 );« is nonsense.)

Why is it nonsense?

> . Above, AFAIK, the primitve "abc" was implicitly converted
> into an object /three times/.

Not necessarily as *these* expressions can be optimized by a compiler to

"a"
"b"
"c"

respectively.

> If one would have written:
>
> { let abc = new String( "abc" );
> abc.charAt( 0 );
> abc.charAt( 1 );
> abc.charAt( 2 ); }
>
> only one object would have been created.

Not necessarily. In fact, that you are declaring the variable in block
scope using â??letâ? makes it possible to optimize here easily as there is no
code outside of the block that could change the value of *that* â??abcâ?.
Assuming, of course, that â??Stringâ? still has its initial value.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Andreas Bergmaier

6/2/2015 9:52:00 PM

0

Thomas 'PointedEars' Lahn schrieb:
> Andreas Bergmaier wrote:
>
>> Current compilers don't convert anything to an
>> object any more here, they just call the native
>> `String.prototype.charAt` method directly on the primitive value.
>
> Cite evidence.

This should get you started:
http://stackoverflow.com/questions/17256182/what-is-the-difference-between-string-literals-and-string-objects-in-...
The speed tests (http://jsperf.com/heera-string-literal...)
linked from there speak for themselves.
You can read yourself through the V8 code, they do distinguish between
primitive values and objects a lot to gain extra optimisations.
I couldn't locate the exact code myself, but when you have to look up a
property on a primitive value you can directly jump to the respective
prototype intrinsic object. There's no need to create a wrapper object,
unless the lookup turns out to become a method call of a sloppy-mode
function.

I also found this bug
report:http://code.google.com/p/v8/issues/deta... which seems to
have been forgotten in 2011.

Bergi

Thomas 'PointedEars' Lahn

6/2/2015 11:17:00 PM

0

Andreas Bergmaier wrote:

> Thomas 'PointedEars' Lahn schrieb:
>> Andreas Bergmaier wrote:
>>> Current compilers don't convert anything to an
>>> object any more here, they just call the native
>>> `String.prototype.charAt` method directly on the primitive value.
>> Cite evidence.
>
> This should get you started:
> http://stackoverflow.com/questions/17256182/what-is-the-difference-between-string-literals-and-string-objects-in-...

I do not see anything there that substantiates your claim. Instead, I see
an answer, <http://stackoverflow.com/a/17256419/..., that clearly
contradicts your claim when it says:

| I'll exemplify with V8 but I assume other engines use similar approaches.
|
| A string primitive is parsed to a v8::String object. Hence, methods can be
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^
| invoked directly on it as mentioned by jfriend00.

> The speed tests (http://jsperf.com/heera-string-literal...)
> linked from there speak for themselves.

To quote from one of my favorite books [1]:

| â??/No/ data speaks for itself,â? McCoy said, forceful. â??Data just lies
| there. /People/ speak. The idiom â??speaks for itselfâ?? almost /always/
| translates as â??If I donâ??t say something about this, no one will notice
| it.â?? Sloppy thinking, Selv! You are dealing with second- and third-hand
| data. [â?¦]

I asked you to cite *evidence*, not inference and supposition. Sloppy
thinking, Andreas!

> You can read yourself through the V8 code, [â?¦]

That would be *one* "compiler", and what you cited already indicates that
your claim does not apply to it in particular.

_________
[1] Diane DUANE. Spockâ??s World. Pocket Books, 1986. ISBN 9780671041137.
p. 296
--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Andreas Bergmaier

6/3/2015 4:22:00 AM

0

Thomas 'PointedEars' Lahn schrieb:
> Andreas Bergmaier wrote:
>
>> This should get you started:
>> http://stackoverflow.com/questions/17256182/what-is-the-difference-between-string-literals-and-string-objects-in-...
>
> I do not see anything there that substantiates your claim. Instead, I see
> an answer, <http://stackoverflow.com/a/17256419/..., that clearly
> contradicts your claim when it says:
>
> | I'll exemplify with V8 but I assume other engines use similar approaches.
> |
> | A string primitive is parsed to a v8::String object. Hence, methods can be
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^
> | invoked directly on it as mentioned by jfriend00.

I'd assume you've read the next two sentences as well?

Yes, primitive strings are represented as C++ objects in V8 - because
sequences of characters can become quite complex, and you might want to
intern them and everything. An OOP approach seems suitable here, and I'm
not even sure whether a std::c_str or char* would match your definition
of primitive C++ value.
But still, those v8::String objects are primitive JS values, while in
contrast

| A String object, in the other hand, is parsed to a v8::StringObject
| which extends Object and, apart from being a full fledged object,
| serves as a wrapper for v8::String.

So yes, StringObject objects are C++ objects as well. Understanding the
difference between those two classes, it is clear that using String
objects in JS

| has to unbox this v8::StringObject's v8::String before executing the
| method, hence it is slower.

Bergi

Thomas 'PointedEars' Lahn

6/3/2015 5:58:00 AM

0

Andreas Bergmaier wrote:

> Thomas 'PointedEars' Lahn schrieb:
>> Andreas Bergmaier wrote:
>>> This should get you started:
>>> http://stackoverflow.com/questions/17256182/what-is-the-difference-between-string-literals-and-string-objects-in-...
>>
>> I do not see anything there that substantiates your claim. Instead, I
>> see an answer, <http://stackoverflow.com/a/17256419/..., that clearly
>> contradicts your claim when it says:
>>
>> | I'll exemplify with V8 but I assume other engines use similar
>> | approaches.
>> |
>> | A string primitive is parsed to a v8::String object. Hence, methods can
>> | be
>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^
>> | invoked directly on it as mentioned by jfriend00.
>
> I'd assume you've read the next two sentences as well?

I have, but they are irrelevant to your claim.

> [red herring]

> | has to unbox this v8::StringObject's v8::String before executing the
> | method, hence it is slower.

Your logic is flawed. What you quoted does not change, it rather
*emphasizes*, the fact that string primitives *are* parsed into objects by
V8, which you claimed they would not be:

>>>>>> the primitve "abc" was implicitly converted into an object /three
>>>>>> times/.
>>>>> In the old times, maybe. Current compilers don't convert anything to
>>>>> an object any more here, [â?¦]

â?? Andreas Bergmaier in <news:mkl000$bif$1@news.albasani.net>

V8 is what can be called a â??current script engineâ?. So it contains a
â??current compilerâ?. But that compiler *does* parse string primitives into
(v8::String) objects.

Therefore, _not_ *all* â??current compilers do not convert anything to an
object any more there.�

Your claim has been refuted by your sources.

â??Learn reason above all. Learn clear thought: learn to know
what is from what seems to be, and what you wish to be.�

â??Surak

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

ram

6/3/2015 6:31:00 AM

0

Andreas Bergmaier <andber93@web.de> writes:
>Yes, primitive strings are represented as C++ objects in V8

In the terminology of the language C++, when you define:

int i = 7;

, then this »i« is the name of an object. So, it is hard
to represent something with anything else than an object
in C++.

Martin Honnen

6/3/2015 8:16:00 AM

0

ram@zedat.fu-berlin.de (Stefan Ram) wrote:
> IIRC, I read somewhere that one should prefer to use
> primitives such as "abc"; one should not create objects
> via »new String ( "abc )« (or, »new Number( 7 )«).
>
> I am not sure why »new String( "abc" )« should be bad style.


Well with

var s1 = new String("foo"); var s2 = new String("foo"); s1 == s2

you get "false" while with

var s1 = "foo"; var s2 = "foo"; s1 == s2

you get "true" so it is not necessarily a question of style but of
semantics you would want to have when using your data in expressions.


--- news://freenews.netfront.net/ - complaints: news@netfront.net ---

John Harris

6/3/2015 2:39:00 PM

0

On 3 Jun 2015 06:31:08 GMT, ram@zedat.fu-berlin.de (Stefan Ram) wrote:

>Andreas Bergmaier <andber93@web.de> writes:
>>Yes, primitive strings are represented as C++ objects in V8
>
> In the terminology of the language C++, when you define:
>
>int i = 7;
>
> , then this »i« is the name of an object. So, it is hard
> to represent something with anything else than an object
> in C++.

That's rather misleading. In the C and C++ standards the word "object"
is defined as follows :
"An object is a region of storage."

When a variable occupies a region of storage then it is said to be an
object. This has nothing to do with OO, especially in C.

John