TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

WebKit sorts JS arrays using Selection Sort

53 点作者 lars将近 13 年前

10 条评论

Robin_Message将近 13 年前
It appears to me from the line:<p><pre><code> 631 if (thisObj-&#62;classInfo() == &#38;JSArray::s_info &#38;&#38; !asArray(thisObj)-&#62;inSparseMode()) { </code></pre> that only non-arrays, or arrays in some kind of "sparse" mode are sorted inefficiently.<p>I'm not sure what sparse mode is, but let's try my Raymond Chen inspired psychic powers: if you assign a[0]=1 and a[1000000]=2, you don't want a 1 to 999999 to be stored, so an array like that will end up in a sparse mode which functions more like a hash table keyed by integers.<p>The same applies to non-arrays: since they aren't true arrays, they won't be stored in the blessed, contiguous way that the sort routine is expecting, so a slower access method has to be used.<p>Now, fast sorting is presumably written assuming the array is in a contiguous array. Since in sparse mode the array isn't like that, we aren't on the fast path, so it doesn't matter if we are slow, and correctness and special cases are more important, hence the simple selection sort algorithm.<p>Note also that line 633, 635 and 637 further specialise the fast path into three cases: a default sort with no user-defined comparator, a sort based on a built-in numerical comparator, and a more general sort using a user-defined comparator. (EDIT: The functions called on 633, 635 and 637 are defined in <a href="http://trac.webkit.org/browser/trunk/Source/JavaScriptCore/runtime/JSArray.cpp" rel="nofollow">http://trac.webkit.org/browser/trunk/Source/JavaScriptCore/r...</a> from line 1409 onwards, and use quicksort or mergesort, except in the general case which uses an AVL tree, which feels odd but I'm sure there was a reason.)<p>TL;DR: This only applies to certain arrays; the performance is generally fine else <i>someone would have noticed by now</i>; if you are sorting array-like things or sparse arrays regularly, profile to discover if this is a problem for you.
评论 #4239671 未加载
评论 #4239926 未加载
评论 #4241331 未加载
评论 #4240369 未加载
js2将近 13 年前
FWIW, this code's over a decade old. Presumably if it had any negative performance implications, someone would have noticed by now:<p><pre><code> author kocienda &#60;kocienda@&#62; Fri, 24 Aug 2001 10:24:45 -0400 (14:24 +0000) Imported sources from kde-2.2 distribution </code></pre> <a href="http://git.chromium.org/gitweb/?p=external/Webkit.git;a=blob;f=JavaScriptCore/kjs/array_object.cpp;hb=66a6d360850d1643dc51807f9c6c0e0029ce3459#l292" rel="nofollow">http://git.chromium.org/gitweb/?p=external/Webkit.git;a=blob...</a>
评论 #4240057 未加载
评论 #4239621 未加载
mda将近 13 年前
V8 sort algorithm (Quicksort + Insertion Sort):<p><a href="http://code.google.com/p/v8/source/browse/trunk/src/array.js#741" rel="nofollow">http://code.google.com/p/v8/source/browse/trunk/src/array.js...</a>
评论 #4239568 未加载
EchoAbstract将近 13 年前
Does any browser actually rely on WebKit's JS Core? I know Safari has Nitro and Chrome has v8, so I'm not sure what would use this code except maybe konqueror.
评论 #4239702 未加载
mbostock将近 13 年前
This is part of the reason why I ported Vladimir Yaroslavskiy's dual-pivot quicksort to JavaScript (<a href="https://github.com/square/crossfilter/blob/master/src/quicksort.js" rel="nofollow">https://github.com/square/crossfilter/blob/master/src/quicks...</a>) for Crossfilter (<a href="http://square.github.com/crossfilter" rel="nofollow">http://square.github.com/crossfilter</a>). It is much, much faster than the native sort. I would guess that WebKit uses this slower algorithm because it's an easy way to implement stable sort. If I recall, Chrome was one of the earlier browsers to implement a non-stable sorting algorithm for JavaScript and it broke various pages that assumed stable sort (such as tables that let you sort by multiple columns).<p>Fun side-note: you can use a matrix diagram to visualize how your browser sorts, letting you determine your browser's sort algorithm visually. For example, Chrome uses median-of-3 quicksort, and Firefox uses mergesort. See for yourself!<p><a href="http://bost.ocks.org/mike/shuffle/compare.html" rel="nofollow">http://bost.ocks.org/mike/shuffle/compare.html</a><p><a href="http://www.flickr.com/photos/mbostock/sets/72157628971703067/detail/" rel="nofollow">http://www.flickr.com/photos/mbostock/sets/72157628971703067...</a>
评论 #4241617 未加载
damian2000将近 13 年前
I don't know much JS but shouldn't there be a quicksort library function for that?
rodneyrehm将近 13 年前
<a href="http://blog.rodneyrehm.de/archives/14-Sorting-Were-Doing-It-Wrong.html#stability" rel="nofollow">http://blog.rodneyrehm.de/archives/14-Sorting-Were-Doing-It-...</a> may be of [some] interest here, too.
jere将近 13 年前
&#62; // "Min" sort. Not the fastest, but definitely less code than heapsort // or quicksort, and much less swapping than bubblesort/insertionsort.<p>Oh, OK. Glad you could save yourself some time/LOC.
评论 #4239754 未加载
danso将近 13 年前
FWIW, this animated demo of the comparative sorting efficiencies <a href="http://www.sorting-algorithms.com/" rel="nofollow">http://www.sorting-algorithms.com/</a>
lucian1900将近 13 年前
Interesting. Perhaps a quicksort/heapsort/mergesort should be written in JS instead and used by webkit for large collections. It'd be a lot less code than in C++, that's for sure.
评论 #4239519 未加载
评论 #4239545 未加载