js split

JavaScript performance comparison

Test case created by termi

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var _String_split = String.prototype.split
    , _String_split_shim_isnonparticipating = /()??/.exec("")[1] === void 0;
   
    function nativeSplit(str, separator) {
      return str.split(separator)
    }
   
    function jsSplit(str, separator, limit) {
                // if `separator` is not a regex, use the native `split`
                if(!(separator instanceof RegExp)) {//if (Object.prototype.toString.call(separator) !== "[object RegExp]") {
                        //http://es5.github.com/#x15.5.4.14
                        //If separator is undefined, then the result array contains just one String, which is the this value (converted to a String). If limit is not undefined, then the output array is truncated so that it contains no more than limit elements.
                        if(separator === void 0 && limit === 0)return [];
   
                        return _String_split.call(str, separator, limit);
                }
   
                var output = []
                        , flags = (separator["ignoreCase"] ? "i" : "") +
                                        (separator["multiline"] ? "m" : "") +
                        (separator["extended"] ? "x" : "") + // Proposed for ES6
                        (separator["sticky"]   ? "y" : "") // Firefox 3+
                        , lastLastIndex = 0
                // Make `global` and avoid `lastIndex` issues by working with a copy
                        , separator2
                        , match
                        , lastIndex
                        , lastLength
                ;
   
                separator = new RegExp(separator.source, flags + "g");
   
                str += ""; // Type-convert
                if (!_String_split_shim_isnonparticipating) {
                // Doesn't need flags gy, but they don't hurt
                        separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags);
                }
   
                /* Values for `limit`, per the spec:
             * If undefined: 4294967295 // Math.pow(2, 32) - 1
             * If 0, Infinity, or NaN: 0
             * If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296;
             * If negative number: 4294967296 - Math.floor(Math.abs(limit))
             * If other: Type-convert, then use the above rules
             */

            limit = limit === void 0 ?
                -1 >>> 0 : // Math.pow(2, 32) - 1
                limit >>> 0 // ToUint32(limit)
                ;
   
                if (!limit) {
                        return [];
                }
   
                while (match = separator.exec(str)) {
                // `separator.lastIndex` is not reliable cross-browser
                lastIndex = match.index + match[0].length;
   
                        if (lastIndex > lastLastIndex) {
                                output.push(str.slice(lastLastIndex, match.index));
   
                                // Fix browsers whose `exec` methods don't consistently return `undefined` for
                    // nonparticipating capturing groups
                                // __ NOT WORKING __ !!!!
                                if (!_String_split_shim_isnonparticipating && match.length > 1) {
                                        match[0].replace(separator2, function() {
                                                for (var i = 1, l = arguments.length - 2; i < l; i++) {
                                                        if (arguments[i] === void 0) {
                                                                match[i] = void 0;
                                                        }
                                                }
                                        });
                                }
   
                                if (match.length > 1 && match.index < str.length) {
                                        output.push.apply(output, match.slice(1));
                                }
   
                                lastLength = match[0].length;
                                lastLastIndex = lastIndex;
   
                                if (output.length >= limit) {
                                        break;
                                }
                        }
   
                        if (separator.lastIndex === match.index) {
                                separator.lastIndex++; /// Avoid an infinite loop
                        }
                }
   
                if (lastLastIndex === str.length) {
                        if (lastLength || !separator.test("")) {
                                output.push("");
                        }
                } else {
                        output.push(str.slice(lastLastIndex));
                }
   
                return output.length > limit ? output.slice(0, limit) : output;
        }
   
    var smalText = "test, test,test, test,test, test,test, test,test, test";
   
    var largeText = "Sed ut perspiciatis unde, omnis iste, natus error sit voluptatem, accusantium doloremque, , ,  laudantium, totam rem aperiam, eaque ipsa quae, ab, illo inventore veritatis et, quasi architecto beatae vitae dicta sunt, explicabo. Nemo enim, ipsam, voluptatem quia voluptas sit aspernatur, aut, odit aut fugit, sed, quia, consequuntur, magni, dolores, eos, qui, ratione, voluptatem sequi nesciunt. Neque, porro quisquam est, qui, dolorem, ipsum, quia, dolor, sit, amet, consectetur, adipisci velit, sed, qui,a, no,n, numquam, eius,  , modi, tempora, incidunt, ut, labore et dolore magnam aliquam quaerat voluptatem. Ut, enim, ad minima, veniam, quis nostrum exercitationem ullam, , ,   , corporis,  suscipit, laboriosam, nisi, ut aliquid ex ea commodi, consequatur? Quis autem vel, eum, iure, reprehenderit qui in ea voluptate velit, esse quam nihil molestiae, consequatur, vel illum qui dolorem eum fugiat quo, voluptas nulla pariatur?, test ,  , test12 , 12 , , 2,33, , 4Sed ut perspiciatis unde, omnis iste, natus error sit voluptatem, accusantium doloremque, , ,  laudantium, totam rem aperiam, eaque ipsa quae, ab, illo inventore veritatis et, quasi architecto beatae vitae dicta sunt, explicabo. Nemo enim, ipsam, voluptatem quia voluptas sit aspernatur, aut, odit aut fugit, sed, quia, consequuntur, magni, dolores, eos, qui, ratione, voluptatem sequi nesciunt. Neque, porro quisquam est, qui, dolorem, ipsum, quia, dolor, sit, amet, consectetur, adipisci velit, sed, qui,a, no,n, numquam, eius,  , modi, tempora, incidunt, ut, labore et dolore magnam aliquam quaerat voluptatem. Ut, enim, ad minima, veniam, quis nostrum exercitationem ullam, , ,   , corporis,  suscipit, laboriosam, nisi, ut aliquid ex ea commodi, consequatur? Quis autem vel, eum, iure, reprehenderit qui in ea voluptate velit, esse quam nihil molestiae, consequatur, vel illum qui dolorem eum fugiat quo, voluptas nulla pariatur?Sed ut perspiciatis unde, omnis iste, natus error sit voluptatem, accusantium doloremque, , ,  laudantium, totam rem aperiam, eaque ipsa quae, ab, illo inventore veritatis et, quasi architecto beatae vitae dicta sunt, explicabo. Nemo enim, ipsam, voluptatem quia voluptas sit aspernatur, aut, odit aut fugit, sed, quia, consequuntur, magni, dolores, eos, qui, ratione, voluptatem sequi nesciunt. Neque, porro quisquam est, qui, dolorem, ipsum, quia, dolor, sit, amet, consectetur, adipisci velit, sed, qui,a, no,n, numquam, eius,  , modi, tempora, incidunt, ut, labore et dolore magnam aliquam quaerat voluptatem. Ut, enim, ad minima, veniam, quis nostrum exercitationem ullam, , ,   , corporis,  suscipit, laboriosam, nisi, ut aliquid ex ea commodi, consequatur? Quis autem vel, eum, iure, reprehenderit qui in ea voluptate velit, esse quam nihil molestiae, consequatur, vel illum qui dolorem eum fugiat quo, voluptas nulla pariatur?Sed ut perspiciatis unde, omnis iste, natus error sit voluptatem, accusantium doloremque, , ,  laudantium, totam rem aperiam, eaque ipsa quae, ab, illo inventore veritatis et, quasi architecto beatae vitae dicta sunt, explicabo. Nemo enim, ipsam, voluptatem quia voluptas sit aspernatur, aut, odit aut fugit, sed, quia, consequuntur, magni, dolores, eos, qui, ratione, voluptatem sequi nesciunt. Neque, porro quisquam est, qui, dolorem, ipsum, quia, dolor, sit, amet, consectetur, adipisci velit, sed, qui,a, no,n, numquam, eius,  , modi, tempora, incidunt, ut, labore et dolore magnam aliquam quaerat voluptatem. Ut, enim, ad minima, veniam, quis nostrum exercitationem ullam, , ,   , corporis,  suscipit, laboriosam, nisi, ut aliquid ex ea commodi, consequatur? Quis autem vel, eum, iure, reprehenderit qui in ea voluptate velit, esse quam nihil molestiae, consequatur, vel illum qui dolorem eum fugiat quo, voluptas nulla pariatur?";
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Native split smalText
var it = largeText.length / smalText.length;
while(--it >= 0)nativeSplit(smalText, /(,\s*)/)
pending…
JS split smalText
var it = largeText.length / smalText.length;
while(--it >= 0)jsSplit(smalText, /(,\s*)/)
pending…
Native split largeText
nativeSplit(largeText , /(,\s*)/)
pending…
JS split largeText
jsSplit(largeText, /(,\s*)/)
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment