fork download
  1. process.stdin.resume();
  2. process.stdin.setEncoding('utf8');
  3.  
  4. var iters = 1000000,
  5. counter = 1,
  6. string = "abc0123$%^&*фываЁ",
  7. etalon = withRegex( string );
  8.  
  9. function withRegex( string ) {
  10. return string.replace( /[^a-z0-9а-яё]/ig, '' );
  11. };
  12.  
  13. function withRegex2( string ) {
  14. return string.replace( /[^a-z0-9а-яё]+/ig, '' );
  15. };
  16.  
  17. function withPureJS( string ) {
  18. var result = '',
  19. length = string.length;
  20. for ( var pos = 0; pos < string.length; pos++ ) {
  21. result += parseChar(string.charCodeAt(pos))
  22. };
  23. return result;
  24. };
  25. function parseChar(c) {
  26. if(( c > 47 && c < 58 ) || ( c > 64 && c < 91 ) || ( c > 96 && c < 123 ) ||
  27. ( c > 1071 && c < 1104 ) || ( c > 1039 && c < 1072 ) || c == 1105 || c == 1025) {
  28. return String.fromCharCode(c);
  29. }
  30. return '';
  31. }
  32.  
  33. function test( func, string, iters, etalon ) {
  34. var tStart = Date.now();
  35. for ( var i=0; i<iters; i++ ) {
  36. if ( func( string ) !== etalon ) {
  37. console.log( "error" );
  38. break;
  39. };
  40. };
  41. var tEnd = Date.now();
  42. console.log( "test " + counter++, tEnd - tStart );
  43. };
  44.  
  45. console.log( "Test started" );
  46. test( withRegex, string, iters, etalon );
  47. test( withRegex2, string, iters, etalon );
  48. test( withPureJS, string, iters, etalon );
Success #stdin #stdout 2.6s 52064KB
stdin
Standard input is empty
stdout
Test started
test 1 649
test 2 584
test 3 1237