admin管理员组

文章数量:1336660

I want to find the number of tabs at the beginning of a string (and of course I want it to be fast running code ;) ). This is my idea, but not sure if this is the best/fastest choice:

//The regular expression
var findBegTabs = /(^\t+)/g;

//This string has 3 tabs and 2 spaces: "<tab><tab><space>something<space><tab>"
var str = "      something  ";

//Look for the tabs at the beginning
var match = reg.exec( str );

//We found...
var numOfTabs = ( match ) ? match[ 0 ].length : 0;

Another possibility is to use a loop and charAt:

//This string has 3 tabs and 2 spaces: "<tab><tab><space>something<space><tab>"
var str = "      something  ";

var numOfTabs = 0;
var start = 0;

//Loop and count number of tabs at beg
while ( str.charAt( start++ ) == "\t" ) numOfTabs++;

I want to find the number of tabs at the beginning of a string (and of course I want it to be fast running code ;) ). This is my idea, but not sure if this is the best/fastest choice:

//The regular expression
var findBegTabs = /(^\t+)/g;

//This string has 3 tabs and 2 spaces: "<tab><tab><space>something<space><tab>"
var str = "      something  ";

//Look for the tabs at the beginning
var match = reg.exec( str );

//We found...
var numOfTabs = ( match ) ? match[ 0 ].length : 0;

Another possibility is to use a loop and charAt:

//This string has 3 tabs and 2 spaces: "<tab><tab><space>something<space><tab>"
var str = "      something  ";

var numOfTabs = 0;
var start = 0;

//Loop and count number of tabs at beg
while ( str.charAt( start++ ) == "\t" ) numOfTabs++;
Share Improve this question edited Sep 14, 2011 at 18:11 Don Rhummy asked Sep 14, 2011 at 17:33 Don RhummyDon Rhummy 25.9k53 gold badges192 silver badges362 bronze badges 3
  • You are using different variable names in the first example, as well as having a typo with ) ). – pimvdb Commented Sep 14, 2011 at 17:41
  • sorry, I just wrote it in the textarea of stackoverflow. I'll fix. – Don Rhummy Commented Sep 14, 2011 at 18:11
  • Also, now, in ES6, you may use a regex like: '\t\tabc'.match(/\t/gy) || '').length – Wiktor Stribiżew Commented Oct 3, 2019 at 8:54
Add a ment  | 

3 Answers 3

Reset to default 5

In general if you can calculate the data by simply iterating through the string and doing a character check at every index, this will be faster than a regex/regular expression which must build up a more plex searching engine. I encourage you to profile this but I think you'll find the straight search is faster.

Note: Your search should use === instead of == here as you don't need to introduce conversions in the equality check.

function numberOfTabs(text) {
  var count = 0;
  var index = 0;
  while (text.charAt(index++) === "\t") {
    count++;
  }
  return count;
}

Try using a profiler (such as jsPerf or one of the many available backend profilers) to create and run benchmarks on your target systems (the browsers and/or interpreters you plan to support for your software).

It's useful to reason about which solution will perform best based on your expected data and target system(s); however, you may sometimes be surprised by which solution actually performs fastest, especially with regard to big-oh analysis and typical data sets.

In your specific case, iterating over characters in the string will likely be faster than regular expression operations.

One-liner (if you find smallest is best):

"\t\tsomething".split(/[^\t]/)[0].length;

i.e. splitting by all non-tab characters, then fetching the first element and obtaining its length.

本文标签: javascriptWhat39s the bestfastest way to find the number of tabs to start a stringStack Overflow