From: Evertjan. on
Lasse Reichstein Nielsen wrote on 26 jun 2010 in comp.lang.javascript:

>> Currently, I'm doing:
>>
>> varArray = varValue.split(";");
>> varEls = varArray.length;
>>
>> Is this reliable, or is there a better way?
>
> If there are no other semicolons than the ones separating your
> elements, it should split the string into those elements.
>
> It's a waste of time and space to make all those small strings
> if you don't need them.
> You could also do something like this:
>
> var count = 0;
> for (var i = varArray.indexOf(";");
> i >= 0;
> i = varArray.indexOf(";", i + 1)){
> count++;
> }
>

Do you hold that that uses less executing time and scripting space than

var numberOfElements = varString.split(/;/).length;

or

var numberOfElements = varString.replace(/[^;]+/g,'').length+1;

or not counting empty elements:

var numberOfElements = varString.match(/[^;]+/g).length;

?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
From: Thomas 'PointedEars' Lahn on
Lasse Reichstein Nielsen wrote:

> HerbF(a)earthlink.net writes:
>> varArray = varValue.split(";");
>> varEls = varArray.length;
>>
>> Is this reliable, or is there a better way?
>
> If there are no other semicolons than the ones separating your
> elements, it should split the string into those elements.
>
> It's a waste of time and space to make all those small strings
> if you don't need them.
> You could also do something like this:
>
> var count = 0;
> for (var i = varArray.indexOf(";");
> i >= 0;
> i = varArray.indexOf(";", i + 1)){
> count++;
> }

May I suggest

var count = (varArray.match(/;/g) || "").length + 1;

instead? ;-)


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
From: Lasse Reichstein Nielsen on
"Evertjan." <exjxw.hannivoort(a)interxnl.net> writes:

> Lasse Reichstein Nielsen wrote on 26 jun 2010 in comp.lang.javascript:

>> You could also do something like this:
>>
>> var count = 0;
>> for (var i = varArray.indexOf(";");
>> i >= 0;
>> i = varArray.indexOf(";", i + 1)){
>> count++;
>> }
>>
>
> Do you hold that that uses less executing time and scripting space than
>
> var numberOfElements = varString.split(/;/).length;

RegExp should be global.

>
> or
>
> var numberOfElements = varString.replace(/[^;]+/g,'').length+1;
>
> or not counting empty elements:
>
> var numberOfElements = varString.match(/[^;]+/g).length;
>
> ?

Time, probably not, but space, yes.
When you split a string, you need to create all the small substrings.
When you replace parts of a string, you need to create the new string.
When you do a global regexp match, you again need to create a string
for each of the matches.

All this string copying around will cost you as well.

(If anything, I'd prefer
var count = str.replace(/[^;]+/g,"").length + 1;
since its only copying the delimiters, not the content :)

/L
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

From: Asen Bozhilov on
Thomas 'PointedEars' Lahn wrote:
> Lasse Reichstein Nielsen wrote:

> >  var count = 0;
> >  for (var i = varArray.indexOf(";");
> >       i >= 0;
> >       i = varArray.indexOf(";", i + 1)){
> >    count++;
> >  }
>
> May I suggest
>
>   var count = (varArray.match(/;/g) || "").length + 1;

OP does not give definition what should mean an element. If element
has fixed characters length, can be used:

Math.ceil(str.length / (elSize + 1));

Otherwise your approach is enough. Of course he should know if use
your approach, semicolon cannot appear in element. He must define,
what is an element before want solution of the problem.

From: Evertjan. on
Lasse Reichstein Nielsen wrote on 26 jun 2010 in comp.lang.javascript:

>> var numberOfElements = varString.split(/;/).length;
>
> RegExp should be global.
>

Please explain.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)