Re: syntax question

Wednesday, September 5, 2012

If you want to start adding in alternatives, and talk about what is best... why leave out a straight check for equality?

If the goal is really to simply check to see if an arbitrary string is "<b>" or "<i>", the most understandable and (more importantly) correct check is simply...
(str == "<b>") || (str == "<i>")

It is also one of the fastest options (by far the fastest on Chrome).

- Joe

On Wed, Sep 5, 2012 at 2:48 AM, Torsten Walter <> wrote:
Hi Erik,

I agree with your assessments.
However, the problem you point out is of course one you should avoid in the first place:

Never augment Object.prototype unless you absolutely have to.
I understand the point of your example though.

What I was trying to offer as a solution was having an object literal (or JSON or whatever), basically a list of keys with a bool value assigned.

This should (in Theory) beat Expressions and other comparisons since it only evaluates once to a true boolean value.

However, I am totally with you that concise and maintainable code is the most important. Don't optimize too early.

For most use cases a `Regexp.test()` is easier to understand than others.


On Wed, Sep 5, 2012 at 12:05 AM, Erik Eckhardt <> wrote:
Hi Torsten,

Thanks for joining the conversation.

It is very interesting that the object member method is so much faster, but it has the same problem as the other object-based methods--it isn't reliable:

Object.prototype['<b>'] = true;
[]['<b>'] || false;
Object.prototype['<b>'] = true;
[]['<b>'] || false; // returns true

Also, IE 7 cannot run your test case since it doesn't have Array.indexOf. IE also kept throwing warnings about unresponsive script so I'm sure that will interfere with the testing anyway.

There simply is no clear performance winner here. In any case is most correct, clear, and concise (in that order) is more important than performance (until a true bottleneck is found). I find the correctness of the object methods suspect, leaving regular expressions or Array.indexOf better candidates, in my mind.

Object.prototype['<b>'] = true;
[]['<b>'] || false;
Object.prototype['<b>'] = true;
[]['<b>'] || false;

On Tue, Sep 4, 2012 at 1:01 PM, Torsten Walter <> wrote:
I've put up a test case on jsPerf:

Depending on the engine you might see varying results. You will also notice that your examples aren't the most efficient ones.

The fastest on Safari 6 is the member lookup.
The str in obj is slightly slower.
Regular expressions were the slowest in my case. You can get rid of the positive lookahead if you want exact match anyway to speed it up slightly.

I don't have the setup right here to test more browsers at the moment.


On Sun, Aug 26, 2012 at 8:29 PM, thor <> wrote:
Thank you, illandril!  :-)

On Saturday, August 25, 2012 12:12:12 PM UTC-5, illandril wrote:
With my tests...
Chrome 22: regular expressions
IE9: str in obj
FF11: str in obj

On Friday, August 24, 2012 10:53:05 PM UTC-4, thor wrote:
Which do you think is more efficient when checking if a string is either "<i>" or "<b>"?

  var obj = {'<b>': null, '<i>': null};
  var str = '<i>';

  var tag =<(?:b|i)>/);


  var tag = (str in obj);


You received this message because you are subscribed to the Google Groups "JavaScript Forum" group.
To view this discussion on the web visit

To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at





Post a Comment

Note: Only a member of this blog may post a comment.

Copyright © 2010 Web Design | Free Blogger Templates by Splashy Templates | Layout by Atomic Website Templates

Vida de bombeiro Recipes Informatica Humor Jokes Mensagens Curiosity Saude Video Games Animals Diario das Mensagens Eletronica Rei Jesus News Noticias da TV Artesanato Esportes Noticias Atuais Games Pets Career Religion Recreation Business Education Autos Academics Style Television Programming Motosport Humor News The Games Home Downs World News Internet Design Entertaimment Celebrities 1001 Games Doctor Pets Net Downs World Enter Jesus Mensagensr Android Rub Letras Dialogue cosmetics Genexus lasofia thebushrajr wingshock tripedes gorduravegetal dainfamia dejavu-transpersonal jsbenfica republicadasbadanas ruiherbon iranianforaryans eaystcheyl fotosdanadir Só Humor Curiosity Gifs Medical Female American Health Madeira Designer PPS Divertidas Estate Travel Estate Writing Computer Matilde Ocultos Matilde futebolcomnoticias girassol lettheworldturn topdigitalnet Bem amado enjohnny produceideas foodasticos cronicasdoimaginario downloadsdegraca compactandoletras newcuriosidades blogdoarmario arrozinhoii