get the last character

JavaScript performance comparison

Revision 4 of this test case created by Callum Locke

Preparation code

<script>

function charAt(s) {
  return s.charAt(s.length - 1) === '#';
}

function index(s) {
  return s[s.length - 1] === '#';
}

function regex(s) {
  return /#$/.test(s);
}

var POUND_END_RE = /#$/;
function regex_cache(s) {
  return POUND_END_RE.test(s);
}

function substring(s) {
  return s.substr(s.length - 1);
}

function split(s) {
  return s.split('').pop();
}
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
charAt
charAt('foobar');
charAt('http://www.regular-expressions.info/refadv.html');
charAt('https://a.alipayobjects.com/ar/alipay.auth.checkcode-1.1.js#');
pending…
regex
regex('foobar');
regex('http://www.regular-expressions.info/refadv.html');
regex('https://a.alipayobjects.com/ar/alipay.auth.checkcode-1.1.js#');
pending…
regex_cache
regex_cache('foobar');
regex_cache('http://www.regular-expressions.info/refadv.html');
regex_cache('https://a.alipayobjects.com/ar/alipay.auth.checkcode-1.1.js#');
pending…
Substring
substring('foobar');
substring('http://www.regular-expressions.info/refadv.html');
substring('https://a.alipayobjects.com/ar/alipay.auth.checkcode-1.1.js#');
pending…
Split and pop
split('foobar');
split('http://www.regular-expressions.info/refadv.html');
split('https://a.alipayobjects.com/ar/alipay.auth.checkcode-1.1.js#');
pending…
Look up by index
index('foobar');
index('http://www.regular-expressions.info/refadv.html');
index('https://a.alipayobjects.com/ar/alipay.auth.checkcode-1.1.js#');
pending…

Compare results of other browsers

Revisions

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:

0 comments

Add a comment