javascript trim implementation with regexp

本文介绍了由Steven Levithan提出的几种JavaScript字符串修剪方法及其性能对比。这些方法使用正则表达式来去除字符串首尾的空白字符。

there is some research by Steven Levithan, who ventured to find various kind of way to implement the code that do string trimming. 

 

 

he published his algorithms on this link:

 

http://blog.stevenlevithan.com/archives/faster-trim-javascript

 

 

Below shows some of the implementaiton that uses regular expression to do the trimming.

 

/**************************************
*@Summary
*  various kind of the string.trim method 
*
* 
* @Usage:
*   
* compress( "foo=1&foo=2&blah=a&blah=b&foo=3" ) == "foo=1,2,3&blah=a,b"
*
* @TODO:
* some more practical use of the string.replac method call 
*  assert("a b c".replace(/a/, function() { return ""; } ) == " b c", "returning an empty result removes a match");
***************************************/

function trim1(str) {
  return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function trim2(str) {
  return str.replace(/^\s+|\s+$/g, '');
}

function trim(str) {
  var str = str.replace(/^\s\s*/, ''), 
    ws = /\s/, i = str.length;
  while (ws.test(str.charAt(--i)));
  return s.splice(0, i + 1);
}

 

there is no reason to do more than one impl if none has competitive advantage over another. below shows a table that compare the performance on different use cases. 

 

 

         Selector  Trim| Document Trim

Trim1 8.7                    2075.8

Trim2 8.5                    3706.7

trim 13.8                    169.4

gyp ERR! configure error gyp ERR! stack Error: 404 status code downloading 32-bit node.lib gyp ERR! stack at Request.<anonymous> (D:\projectcode\gtfw3\OTC-front\otc-service-hall-front\node_modules\node-gyp\lib\install.js:344:20) gyp ERR! stack at Request.emit (node:events:520:35) gyp ERR! stack at Request.onRequestResponse (D:\projectcode\gtfw3\OTC-front\otc-service-hall-front\node_modules\request\request.js:1059:10) gyp ERR! stack at ClientRequest.emit (node:events:508:28) gyp ERR! stack at HTTPParser.parserOnIncomingClient (node:_http_client:780:27) gyp ERR! stack at HTTPParser.parserOnHeadersComplete (node:_http_common:123:17) gyp ERR! stack at TLSSocket.socketOnData (node:_http_client:615:22) gyp ERR! stack at TLSSocket.emit (node:events:508:28) gyp ERR! stack at addChunk (node:internal/streams/readable:559:12) gyp ERR! stack at readableAddChunkPushByteMode (node:internal/streams/readable:510:3) gyp ERR! System Windows_NT 10.0.26100 gyp ERR! command "D:\\projectcode\\gtfw3\\OTC-front\\otc-service-hall-front\\node_modules\\node\\bin\\node.exe" "D:\\projectcode\\gtfw3\\OTC-front\\otc-service-hall-front\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library=" gyp ERR! cwd D:\projectcode\gtfw3\OTC-front\otc-service-hall-front\node_modules\node-sass gyp ERR! node -v v25.1.0 gyp ERR! node-gyp -v v3.8.0 gyp ERR! not ok Build failed with error code: 1 npm WARN @vue/composition-api@1.7.2 requires a peer of vue@>= 2.5 < 2.7 but none is installed. You must install peer dependencies yourself. npm WARN eslint-config-standard@12.0.0 requires a peer of eslint@>=5.0.0 but none is installed. You must install peer dependencies yourself. npm WARN eslint-config-standard@12.0.0 requires a peer of eslint-plugin-node@>=7.0.0 but none is installed. You must install peer dependencies yourself. npm WARN eslint-config-standard@12.0.0 requires a peer of eslint-plugin-promise@>=4.0.0 but none is installed. You must install peer dependencies yourself. npm WARN eslint-config-standard@12.0.0 requires a peer of eslint-plugin-standard@>=4.0.0 but none is installed. You must install peer dependencies yourself. npm WARN otc-service-hall-front@1.0.0 No repository field. npm WARN otc-service-hall-front@1.0.0 No license field. npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.3.3 (node_modules\fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"}) npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules\mocha-webpack\node_modules\fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"}) npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules\webpack-dev-server\node_modules\fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"}) npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules\watchpack-chokidar2\node_modules\fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"}) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! node-sass@4.14.1 postinstall: `node scripts/build.js` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the node-sass@4.14.1 postinstall script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\dell\AppData\Roaming\npm-cache\_logs\2025-10-31T06_31_47_055Z-debug.log
11-01
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值