List types, and the various lookup methods

JavaScript performance comparison

Revision 7 of this test case created

Preparation code

<script>
var SPAN = "SPAN";

function List(items){
this.__container = {};
this.add( items );
}


List.prototype = {


        Constructor: List,
       
        has: function( key ) {
        return this.__container[key] === true;
        },
       
        add: function( items ) {
        var i, l;
                if( items && ( l = items.length ) ) {
               
                        for(i = 0; i < l; ++i){
                        this.__container[items[i]] = true;
                        }
               
                }
                else if( items ) {
                this.__container[items] = true;
                }
       
        },
       
        remove: function( items ) {
        var i, l;
                if( items && ( l = items.length ) ) {
               
                        for(i = 0; i < l; ++i){
                        delete this.__container[items[i]];
                        }
               
                }
                else if( items ) {
                delete this.__container[items];
                }      
               
       
        },
       
        clear: function(){
        this.__container = {};
        },
       
       
        each: function( fn ){
       
       
       
        }


};
  var allowedArr = ["A", "ABBR", "ACRONYM", "ADDRESS", "APPLET", "AREA", "ARTICLE", "ASIDE", "AUDIO", "B", "BASE", "BASEFONT", "BDO", "BGSOUND", "BIG", "BLOCKQUOTE", "BODY", "BR", "BUTTON", "CANVAS", "CAPTION", "CENTER", "CITE", "CODE", "COL", "COLGROUP", "COMMENT", "CUSTOM", "DD", "DEL", "DFN", "DIR", "DIV", "DL", "DT", "EM", "EMBED", "FIELDSET", "FIGCAPTION", "FIGURE", "FONT", "FOOTER", "FORM", "FRAME", "FRAMESET", "HEAD", "HEADER", "HGROUP", "HN", "HR", "HTML", "I", "IFRAME", "IMG", "INPUT", "INS", "ISINDEX", "KBD", "LABEL", "LEGEND", "LI", "LINK", "LISTING", "MAP", "MARK", "MARQUEE", "MEDIA", "MENU", "META", "NAV", "NOBR", "NOFRAMES", "NOSCRIPT", "OBJECT", "OL", "OPTGROUP", "OPTION", "P", "PARAM", "PLAINTEXT", "PRE", "Q", "RT", "RUBY", "S", "SAMP", "SCRIPT", "SECTION", "SELECT", "SMALL", "SOURCE", "SPAN", "STRIKE", "STRONG", "STYLE", "SUB", "SUP", "TABLE", "TBODY", "TD", "TEXTAREA", "TFOOT", "TH", "THEAD", "TITLE", "TR", "TT", "U", "UL", "VAR", "VIDEO", "WBR", "XML", "XMP"];

var wList = new List( allowedArr );

  var allowedObj = {A:1,ABBR:1,ACRONYM:1,ADDRESS:1,APPLET:1,AREA:1,ARTICLE:1,ASIDE:1,AUDIO:1,B:1,BASE:1,BASEFONT:1,BDO:1,BGSOUND:1,BIG:1,BLOCKQUOTE:1,BODY:1,BR:1,BUTTON:1,CANVAS:1,CAPTION:1,CENTER:1,CITE:1,CODE:1,COL:1,COLGROUP:1,COMMENT:1,CUSTOM:1,DD:1,DEL:1,DFN:1,DIR:1,DIV:1,DL:1,DT:1,EM:1,EMBED:1,FIELDSET:1,FIGCAPTION:1,FIGURE:1,FONT:1,FOOTER:1,FORM:1,FRAME:1,FRAMESET:1,HEAD:1,HEADER:1,HGROUP:1,HN:1,HR:1,HTML:1,I:1,IFRAME:1,IMG:1,INPUT:1,INS:1,ISINDEX:1,KBD:1,LABEL:1,LEGEND:1,LI:1,LINK:1,LISTING:1,MAP:1,MARK:1,MARQUEE:1,MEDIA:1,MENU:1,META:1,NAV:1,NOBR:1,NOFRAMES:1,NOSCRIPT:1,OBJECT:1,OL:1,OPTGROUP:1,OPTION:1,P:1,PARAM:1,PLAINTEXT:1,PRE:1,Q:1,RT:1,RUBY:1,S:1,SAMP:1,SCRIPT:1,SECTION:1,SELECT:1,SMALL:1,SOURCE:1,SPAN:1,STRIKE:1,STRONG:1,STYLE:1,SUB:1,SUP:1,TABLE:1,TBODY:1,TD:1,TEXTAREA:1,TFOOT:1,TH:1,THEAD:1,TITLE:1,TR:1,TT:1,U:1,UL:1,VAR:1,VIDEO:1,WBR:1,XML:1,XMP:1};
  var allowedReg = /^(?:A|ABBR|ACRONYM|ADDRESS|APPLET|AREA|ARTICLE|ASIDE|AUDIO|B|BASE|BASEFONT|BDO|BGSOUND|BIG|BLOCKQUOTE|BODY|BR|BUTTON|CANVAS|CAPTION|CENTER|CITE|CODE|COL|COLGROUP|COMMENT|CUSTOM|DD|DEL|DFN|DIR|DIV|DL|DT|EM|EMBED|FIELDSET|FIGCAPTION|FIGURE|FONT|FOOTER|FORM|FRAME|FRAMESET|HEAD|HEADER|HGROUP|HN|HR|HTML|I|IFRAME|IMG|INPUT|INS|ISINDEX|KBD|LABEL|LEGEND|LI|LINK|LISTING|MAP|MARK|MARQUEE|MEDIA|MENU|META|NAV|NOBR|NOFRAMES|NOSCRIPT|OBJECT|OL|OPTGROUP|OPTION|P|PARAM|PLAINTEXT|PRE|Q|RT|RUBY|S|SAMP|SCRIPT|SECTION|SELECT|SMALL|SOURCE|SPAN|STRIKE|STRONG|STYLE|SUB|SUP|TABLE|TBODY|TD|TEXTAREA|TFOOT|TH|THEAD|TITLE|TR|TT|U|UL|VAR|VIDEO|WBR|XML|XMP)$/i
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Using Array with .indexOf
if (allowedArr.indexOf(SPAN) > -1) {}
pending…
Using Object with in operator
if (SPAN in allowedObj) {}
pending…
Using Object with hasOwnProperty
if (allowedObj.hasOwnProperty(SPAN)) {}
pending…
Using a regular expression
if (allowedReg.test(SPAN)) {}
pending…
Object with truthy values
if (allowedObj[SPAN]) {}
pending…
List object
if( wList.has( SPAN ) ){}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment