charAt vs substr vs substring

Edit on GitHub

Speed of methods to get first char of string.

Setup

var test_str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."

Tests

  • charAt

    var first_char = test_str.charAt(0);
  • substr

    var first_char = test_str.substr(0, 1);
  • substring

    var first_char = test_str.substring(0, 1);