差异表现呼吁string对象.localeCompare,构建特制Intl.Collat​​or物体之间? [英] Difference in performance between calling .localeCompare on string objects and constructing a purpose-made Intl.Collator object?

查看:158
本文介绍了差异表现呼吁string对象.localeCompare,构建特制Intl.Collat​​or物体之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需​​要排序的大量字符串。字符串数组,实际上,但这是除了点。什么是不是的是,我需要实现我自己的分拣机的功能,详见链接question.Performance对我来说非常重要。 jFriend00非常有益建议我用 String.prototype.localeCompare 。我整理的阵列100K +元素长,因此性能是pretty重要。在 MDN文档的 .localeCompare ,在效果,它说:

So I need to sort lots of strings. Arrays of strings, actually, but that's besides the point. What isn't is that I need to implement my own sorter function, as detailed in the linked question.Performance is quite important to me. jFriend00 very helpfully suggested that I use String.prototype.localeCompare. The arrays I'm sorting are 100K+ elements long, and so performance is pretty important. On the MDN doc for .localeCompare, under Performance, it says:

当比较大量的字符串,例如在分拣大型阵列,最好是创建一个Intl.Collat​​or对象,并使用由它的比较属性提供的功能

When comparing large numbers of strings, such as in sorting large arrays, it is better to create an Intl.Collator object and use the function provided by its compare property.

使用似乎pretty简单,如下面的jFriend的功能的实现似乎是功能相同:

Using it seems pretty straightforward, and the implementation of jFriend's function as the following would seem to be functionally equivalent:

data = (function(arrE2){
  var nIC = new Intl.Collator,
      cmp = nIC.compare.bind(nIC);

  return arrE2.concat().sort(function(a, b) {
      var comp, i;
      for (i = 0; i < Math.min(a.length, b.length); i++) {
          if ((comp = cmp(a[i], b[i])) !== 0) return comp;
      } 
      return (a.length > b.length) - (a.length < b.length); 
  });
})(data);

(请你纠正我,如果它确实比任何jFriend的解决方案有所不同。)

(Please do correct me if it does anything different than jFriend's solution.)

不过,目前尚不清楚对我来说这是否会产生任何显著卓越的性能,如果是这样,那么如何。 MDN当然可以做一个更好的方式解释工作,因为对于的 Intl.Collat​​or 甚至没有这么多,因为一提表演。所以我刚刚离开我自己的设备。我是一个的n00b,所以我的直觉是相当不值钱,但我能想到这个以增强性能的唯一方法是,如果规范的替代需要整个区域装入存储器再次遍布每个单独的比较,而这种保留所分配的存储器的区域数据存储在对象

However, what isn't clear to me is whether or not this will yield any significantly superior performance, and if so, then how. MDN could certainly do a way better job explaining, since the linked page for Intl.Collator doesn't even make so much as a mention of "performance." So I'm just left to my own devices.. I'm a n00b, so my intuition is fairly worthless, but the only way that I can think for this to augment performance is if the canonical alternative needs to load the whole locale into memory all over again for each individual comparison, while this retains the allocated memory to store the locale data in the object.


  • 在行为上两个相同?

  • 是我的新版本为卓越性能的推移,如果是这样,它是显著?

推荐答案

我有一个类似的问题,发现的这jsperf 的真正有用的。

I'm having a similar issue and found this jsperf really useful.

底线:是的, Intl.Collat​​or 几乎是两倍的速度比 a.localeCompare(B)

Bottom line: Yes, Intl.Collator is almost twice as fast than a.localeCompare(b).

这篇关于差异表现呼吁string对象.localeCompare,构建特制Intl.Collat​​or物体之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆