DEV Community

Bruce Axtens
Bruce Axtens

Posted on • Edited on

1

Reversing a string using .some

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;
}
Enter fullscreen mode Exit fullscreen mode

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;
};
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

As seems often to be the case, at least in my V8 instance, the ES3 version is the faster.

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
🎥 Audio/video file upload with real-time preview
🗣️ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
📤 Export interview's subtitles in VTT format

Read full post

Top comments (0)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay