Remove last item from / separated string

JavaScript performance comparison

Test case created by amoshydra

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var str = 'lorem/ipsum/dolor/sit/amet/consectetur/adipiscing/elit/sed/do/eiusmod/tempor/incididunt/ut/labore/et/dolore/magna/aliqua/ut/enim/ad/minim/veniam/quis/nostrud/exercitation/ullamco/laboris/nisi/ut/aliquip/ex/ea/commodo/consequat/duis/aute/irure/dolor/in/reprehenderit/in/voluptate/velit/esse/cillum/dolore/eu/fugiat/nulla/pariatur/excepteur/sint/occaecat/cupidatat/non/proident/sunt/in/culpa/qui/officia/deserunt/mollit/anim/id/est/laborum/base/resource/collection/video-collection'

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
With Regexp
str.replace(/\/[^/]*$/, '')
pending…
With Regexp of smaller scope
str.replace(/\/[a-z-]*$/, '')
pending…
With for-loop
var index = -1;
for (var i = str.length - 1; i >= 0; i--) {
  if (str[i] === '/') {
    i = index; break;
  }
}

str.substring(0, index);
pending…
With for-loop (initialized length)
var index = -1;
lastIndex = str.length - 1;
for (var i = lastIndex; i > -1; i--) {
  if (str[i] === '/') {
    i = index; break;
  }
}

str.substring(0, index);
pending…
With lastIndexOf and substring
str.substring(0, str.lastIndexOf('/'))
pending…
With lastIndexOf and slice
str.slice(0, str.lastIndexOf('/'))
pending…
With split, slice, join
str.split('/').slice(0, -1).join('/')
pending…
With split, pop, join
var a = str.split('/')
a.pop()
a.join('/')
pending…

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

0 Comments