String Reversal

JavaScript performance comparison

Test case created by Paul Sayre


Comparison of different string reversal methods

Preparation code

Benchmark.prototype.setup = function() {
    var str = 'Bacon ipsum dolor sit amet pork belly venison fatback rump beef, speck shankle tenderloin beef ribs cow sirloin biltong kielbasa. Ground round speck filet mignon pancetta ball tip jowl. Chuck pastrami short ribs, filet mignon sirloin turkey shank pork belly cow drumstick. Capicola shankle meatball, ball tip bresaola short ribs meatloaf shank hamburger venison t-bone kielbasa sausage biltong tri-tip. Salami meatball beef fatback pork chop, venison ham prosciutto kielbasa. Brisket kielbasa drumstick strip steak short ribs tenderloin short loin. Venison rump tail, meatball jerky shankle hamburger drumstick sausage pancetta corned beef flank pork belly brisket pork loin.';

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
split, reverse, join
var rev = str.split('').reverse().join('');
Iterate backwards
var i = str.length,
  rev = '';

while(i--) {
  rev += str.charAt(i);
Iterate backwards, array
var i = str.length,
  rev = [];

while(i--) {
rev = rev.join('');

Compare results of other browsers


You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:


Comment form temporarily disabled.

Add a comment