I was looking at some of my Google Apps Script code that uses .some()
and I thought (as one does), I wonder if that could be used to reverse a string.
This is about as pure ES6 as I can get it.
const Bruce_SomeReverse = (s, rev = "") => { s.split("").some((itm, idx, arr) => { rev = rev + arr[arr.length - 1 - idx]; }); return rev; }
Using Babel I've converted it to ES3 should anyone want to use it there (like in Google Apps Script).
"use strict"; var Bruce_SomeReverse = function Bruce_SomeReverse(s) { var rev = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ""; s.split("").some(function(itm, idx, arr) { rev = rev + arr[arr.length - 1 - idx]; }); return rev; };
Performance-wise this method is very speedy, in the top 5 (using my speed tester):
Sarah_ForOf 986.973 ticks Bruce_Recursive2 2664.535 ticks Bruce_SomeReverse_ES3 3085.19 ticks Bruce_Recursive1 3209.047 ticks Bruce_SomeReverse 3312.393 ticks
As seems often to be the case, at least in my V8 instance, the ES3 version is the faster.
Top comments (0)