简体   繁体   English

查找启动字符串的制表符数量的最佳/最快方法是什么?

[英]What's the best/fastest way to find the number of tabs to start a string?

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: 另一种可能性是使用循环和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++;

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 which much build up a more complex 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). 尝试使用探查器 (例如jsPerf许多可用的后端探查器之一 )在目标系统(您计划支持软件的浏览器和/或解释 )上创建和运行基准测试。

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;

ie splitting by all non-tab characters, then fetching the first element and obtaining its length. 例如,将所有非制表符分开,然后获取第一个元素并获取其长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 识别字符串数字是否可以在浮点(或双精度)中完全表示的最佳/最快方法是什么? - What's the best/fastest way to recognize if a string number is exactly representable in floating point (or double)? 在 JavaScript 中将字符串转换为数字的最快方法是什么? - What's the fastest way to convert String to Number in JavaScript? 在 JavaScript 中平方一个数字的最快方法是什么? - What's the fastest way to square a number in JavaScript? 在JS中找到路线的最快方法是什么? - What's the fastest way to find a route in JS? 在 JavaScript 中将数字转换为字符串的最佳方法是什么? - What's the best way to convert a number to a string in JavaScript? 在特定区域获取 DOM 元素的最佳(最快)方法是什么 - What's best (fastest) way to get DOM elements in specific area 开始验证的最佳方法是什么? - What's the best way to start validation? 在 JavaScript 中计算字符串中行数的最快方法是什么? - What is the fastest way to count the number of lines in a string in JavaScript? 查找特定周长中哪些坐标的最快方法是什么? - What's the fastest way to find which coordinates are in a certain perimeter? 在数组中找到n个最小数字的最快方法是什么? - What's the fastest way to find the n smallest numbers in an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM