I was wondering if there's a need to split at all, because characters are accessible directly using String#charAt
.
This implementation (which is almost the same as yours - only with the split) should be among the fastest.
function reverse(str) {
var result = [];
for (var i = str.length - 1; i >= 0; i--) {
result.push(str.charAt(i));
}
return result.join("");
}
console.log(reverse("abcde"));
According to some benchmark some benchmark, String concatenation is better optimized than Array.join
, it also makes the code cleaner:
function reverse(str) {
var result = "";
for (var i = str.length - 1; i >= 0; i--) {
result += str.charAt(i);
}
return result;
}
console.log(reverse("abcde"));
As a side-note, you can get creative by using Array.prototype.reduce
and allow JavaScript duck-type the String
as an Array
.
function reverse(str) {
return Array.prototype.reduce.call(str, function(result, c) {
return c + result;
}, "");
}
console.log(reverse("Hello world!"));
And going further you can make an ES6 one-liner:
let reverse = str => Array.prototype.reduce.call(str, (result, c) => c + result, "");
console.log(reverse("Hello world!"));
JSFiddle forked from @Andreas: https://jsfiddle.net/kazenorin/6shbv6hs/2/
I was wondering if there's a need to split at all, because characters are accessible directly using String#charAt
.
This implementation (which is almost the same as yours - only with the split) should be among the fastest.
function reverse(str) {
var result = [];
for (var i = str.length - 1; i >= 0; i--) {
result.push(str.charAt(i));
}
return result.join("");
}
console.log(reverse("abcde"));
According to some benchmark, String concatenation is better optimized than Array.join
, it also makes the code cleaner:
function reverse(str) {
var result = "";
for (var i = str.length - 1; i >= 0; i--) {
result += str.charAt(i);
}
return result;
}
console.log(reverse("abcde"));
As a side-note, you can get creative by using Array.prototype.reduce
and allow JavaScript duck-type the String
as an Array
.
function reverse(str) {
return Array.prototype.reduce.call(str, function(result, c) {
return c + result;
}, "");
}
console.log(reverse("Hello world!"));
And going further you can make an ES6 one-liner:
let reverse = str => Array.prototype.reduce.call(str, (result, c) => c + result, "");
console.log(reverse("Hello world!"));
JSFiddle forked from @Andreas: https://jsfiddle.net/kazenorin/6shbv6hs/2/
I was wondering if there's a need to split at all, because characters are accessible directly using String#charAt
.
This implementation (which is almost the same as yours - only with the split) should be among the fastest.
function reverse(str) {
var result = [];
for (var i = str.length - 1; i >= 0; i--) {
result.push(str.charAt(i));
}
return result.join("");
}
console.log(reverse("abcde"));
According to some benchmark, String concatenation is better optimized than Array.join
, it also makes the code cleaner:
function reverse(str) {
var result = "";
for (var i = str.length - 1; i >= 0; i--) {
result += str.charAt(i);
}
return result;
}
console.log(reverse("abcde"));
As a side-note, you can get creative by using Array.prototype.reduce
and allow JavaScript duck-type the String
as an Array
.
function reverse(str) {
return Array.prototype.reduce.call(str, function(result, c) {
return c + result;
}, "");
}
console.log(reverse("Hello world!"));
And going further you can make an ES6 one-liner:
let reverse = str => Array.prototype.reduce.call(str, (result, c) => c + result, "");
console.log(reverse("Hello world!"));
JSFiddle forked from @Andreas: https://jsfiddle.net/kazenorin/6shbv6hs/2/
I was wondering if there's a need to split at all, because characters are accessible directly using String#charAt
.
This implementation (which is almost the same as yours - only with the split) should be among the fastest.
function reverseStringreverse(str) {
var result = [];
for (var i = str.length - 1; i >= 0; i--) {
result.push(str.charAt(i));
}
return result.join("");
}
console.log(reverseStringreverse("abcde"));
HoweverAccording to some benchmark, asString concatenation is better optimized than Array.join
, it also makes the code cleaner:
function reverse(str) {
var result = "";
for (var i = str.length - 1; i >= 0; i--) {
result += str.charAt(i);
}
return result;
}
console.log(reverse("abcde"));
As a side-note, you can get creative by using String#splitArray.prototype.reduce
: and allow JavaScript duck-type the String
as an Array
.
function reverse(str) {
return strArray.split("")prototype.sortreduce.call(str, function(result, c) {
return 1;c + result;
}).join(, "");
}
console.log(reverse("abcdef""Hello world!"));
GoingAnd going further you can make an ES6 one-liner:
let reverse = str => strArray.split("")prototype.sortreduce.call(str, (result, c) => 1).join(c + result, "");
console.log(reverse("abcdef""Hello world!"));
JSFiddle forked from @Andreas: https://jsfiddle.net/kazenorin/6shbv6hs/2/
I was wondering if there's a need to split at all, because characters are accessible directly using String#charAt
.
This implementation (which is almost the same as yours - only with the split) should be among the fastest.
function reverseString(str) {
var result = [];
for (var i = str.length - 1; i >= 0; i--) {
result.push(str.charAt(i));
}
return result.join("");
}
console.log(reverseString("abcde"));
However, as a side-note you can get creative using String#split
:
function reverse(str) {
return str.split("").sort(function() {
return 1;
}).join("");
}
console.log(reverse("abcdef"));
Going further you can make an ES6 one-liner:
let reverse = str => str.split("").sort(c => 1).join("");
console.log(reverse("abcdef"));
I was wondering if there's a need to split at all, because characters are accessible directly using String#charAt
.
This implementation (which is almost the same as yours - only with the split) should be among the fastest.
function reverse(str) {
var result = [];
for (var i = str.length - 1; i >= 0; i--) {
result.push(str.charAt(i));
}
return result.join("");
}
console.log(reverse("abcde"));
According to some benchmark, String concatenation is better optimized than Array.join
, it also makes the code cleaner:
function reverse(str) {
var result = "";
for (var i = str.length - 1; i >= 0; i--) {
result += str.charAt(i);
}
return result;
}
console.log(reverse("abcde"));
As a side-note, you can get creative by using Array.prototype.reduce
and allow JavaScript duck-type the String
as an Array
.
function reverse(str) {
return Array.prototype.reduce.call(str, function(result, c) {
return c + result;
}, "");
}
console.log(reverse("Hello world!"));
And going further you can make an ES6 one-liner:
let reverse = str => Array.prototype.reduce.call(str, (result, c) => c + result, "");
console.log(reverse("Hello world!"));
JSFiddle forked from @Andreas: https://jsfiddle.net/kazenorin/6shbv6hs/2/
I was wondering if there's a need to split at all, because characters are accessible directly using String#charAt
.
This implementation (which is almost the same as yours - only with the split) should be among the fastest.
function reverseString(str) {
var result = [];
for (var i = str.length - 1; i >= 0; i--) {
result.push(str.charAt(i));
}
return result.join("");
}
console.log(reverseString("abcde"));
However, as a side-note you can get creative using String#split
:
function reverse(str) {
return str.split("").sort(function() {
return 1;
}).join("");
}
console.log(reverse("abcdef"));
Going further you can make an ES6 one-liner:
let reverse = str => str.split("").sort(c => 1).join("");
console.log(reverse("abcdef"));
I was wondering if there's a need to split at all, because characters are accessible directly using String#charAt
.
This implementation should be among the fastest.
function reverseString(str) {
var result = [];
for (var i = str.length - 1; i >= 0; i--) {
result.push(str.charAt(i));
}
return result.join("");
}
console.log(reverseString("abcde"));
However, as a side-note you can get creative using String#split
:
function reverse(str) {
return str.split("").sort(function() {
return 1;
}).join("");
}
console.log(reverse("abcdef"));
Going further you can make an ES6 one-liner:
let reverse = str => str.split("").sort(c => 1).join("");
console.log(reverse("abcdef"));
I was wondering if there's a need to split at all, because characters are accessible directly using String#charAt
.
This implementation (which is almost the same as yours - only with the split) should be among the fastest.
function reverseString(str) {
var result = [];
for (var i = str.length - 1; i >= 0; i--) {
result.push(str.charAt(i));
}
return result.join("");
}
console.log(reverseString("abcde"));
However, as a side-note you can get creative using String#split
:
function reverse(str) {
return str.split("").sort(function() {
return 1;
}).join("");
}
console.log(reverse("abcdef"));
Going further you can make an ES6 one-liner:
let reverse = str => str.split("").sort(c => 1).join("");
console.log(reverse("abcdef"));