Javascript / Node.js正则表达式优化:将regexp置于函数之外是否有任何好处?

时间:2015-09-17 10:04:44

标签: javascript regex node.js optimization v8

我检查的大多数Node.js顶层模块总是在模块范围内定义它们的regexp,在使用它的函数之外。

例如,几行来自Busboy,这是Node.js中最快的multipart / form-data解析器:

!SESSION 2015-09-17 11:54:25.321 -----------------------------------------------
eclipse.buildId=4.5.0.I20150603-2000
java.version=1.8.0_60
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=en_US
Framework arguments:  -product org.eclipse.epp.package.jee.product
Command-line arguments:  -os win32 -ws win32 -arch x86_64 -product org.eclipse.epp.package.jee.product

!ENTRY org.eclipse.ant.ui 4 0 2015-09-17 11:54:26.090
!MESSAGE FrameworkEvent ERROR
!STACK 0
org.osgi.framework.BundleException: Could not resolve module: org.eclipse.ant.ui [80]
  Unresolved requirement: Require-Bundle: org.eclipse.ui.editors; bundle-version="[3.2.0,4.0.0)"; resolution:="optional"
    -> Bundle-SymbolicName: org.eclipse.ui.editors; bundle-version="3.9.0.v20150213-1939"; singleton:="true"
       org.eclipse.ui.editors [758]
         Unresolved requirement: Require-Bundle: org.eclipse.ui.ide; bundle-version="[3.5.0,4.0.0)"
           -> Bundle-SymbolicName: org.eclipse.ui.ide; bundle-version="3.11.0.v20150510-1749"; singleton:="true"
              org.eclipse.ui.ide [761]
                Unresolved requirement: Require-Bundle: org.eclipse.equinox.p2.engine; bundle-version="[2.0.0,3.0.0)"
                  -> Bundle-SymbolicName: org.eclipse.equinox.p2.engine; bundle-version="2.4.0.v20150511-1532"; singleton:="true"
                     org.eclipse.equinox.p2.engine [332]
                       Unresolved requirement: Import-Package: org.eclipse.equinox.internal.p2.metadata.repository.io
                         -> Export-Package: org.eclipse.equinox.internal.p2.metadata.repository.io; bundle-symbolic-name="org.eclipse.equinox.p2.metadata.repository"; bundle-version="1.2.200.v20150428-1613"; version="0.0.0"; x-friends:="org.eclipse.equinox.p2.engine"
                            org.eclipse.equinox.p2.metadata.repository [337]
                              Unresolved requirement: Import-Package: org.eclipse.equinox.internal.p2.persistence
                                -> Export-Package: org.eclipse.equinox.internal.p2.persistence; bundle-symbolic-name="org.eclipse.equinox.p2.repository"; bundle-version="2.3.100.v20150428-1613"; version="0.0.0"; x-friends:="org.eclipse.equinox.p2.artifact.repository,  org.eclipse.equinox.p2.engine,  org.eclipse.equinox.p2.metadata.repository,  org.eclipse.equinox.p2.ui.importexport,  org.eclipse.equinox.p2.repository.tools"
                                   org.eclipse.equinox.p2.repository [342]
                                     Unresolved requirement: Import-Package: org.eclipse.equinox.security.storage
  Unresolved requirement: Require-Bundle: org.eclipse.ui.externaltools; bundle-version="[3.2.0,4.0.0)"
    -> Bundle-SymbolicName: org.eclipse.ui.externaltools; bundle-version="3.3.0.v20150302-1002"; singleton:="true"
       org.eclipse.ui.externaltools [759]
         Unresolved requirement: Require-Bundle: org.eclipse.ui.ide; bundle-version="[3.2.0,4.0.0)"; resolution:="optional"
           -> Bundle-SymbolicName: org.eclipse.ui.ide; bundle-version="3.11.0.v20150510-1749"; singleton:="true"
         Unresolved requirement: Require-Bundle: org.eclipse.debug.ui; bundle-version="[3.10.0,4.0.0)"
           -> Bundle-SymbolicName: org.eclipse.debug.ui; bundle-version="3.11.0.v20150514-0917"; singleton:="true"
              org.eclipse.debug.ui [233]
                Unresolved requirement: Require-Bundle: org.eclipse.ui.ide; bundle-version="[3.5.0,4.0.0)"
                  -> Bundle-SymbolicName: org.eclipse.ui.ide; bundle-version="3.11.0.v20150510-1749"; singleton:="true"
  Unresolved requirement: Require-Bundle: org.eclipse.ui.ide; bundle-version="[3.2.0,4.0.0)"; resolution:="optional"
    -> Bundle-SymbolicName: org.eclipse.ui.ide; bundle-version="3.11.0.v20150510-1749"; singleton:="true"

    at org.eclipse.osgi.container.Module.start(Module.java:434)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1582)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1561)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.doContainerStartLevel(ModuleContainer.java:1533)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1476)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1)
    at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
    at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)

!ENTRY org.eclipse.compare 4 0 2015-09-17 11:54:26.097
!MESSAGE FrameworkEvent ERROR
!STACK 0
org.osgi.framework.BundleException: Could not resolve module: org.eclipse.compare [84]
  Unresolved requirement: Require-Bundle: org.eclipse.ui.ide; bundle-version="[3.3.0,4.0.0)"
    -> Bundle-SymbolicName: org.eclipse.ui.ide; bundle-version="3.11.0.v20150510-1749"; singleton:="true"
       org.eclipse.ui.ide [761]
         Unresolved requirement: Require-Bundle: org.eclipse.equinox.p2.engine; bundle-version="[2.0.0,3.0.0)"
           -> Bundle-SymbolicName: org.eclipse.equinox.p2.engine; bundle-version="2.4.0.v20150511-1532"; singleton:="true"
              org.eclipse.equinox.p2.engine [332]
                Unresolved requirement: Import-Package: org.eclipse.equinox.internal.p2.metadata.repository.io
                  -> Export-Package: org.eclipse.equinox.internal.p2.metadata.repository.io; bundle-symbolic-name="org.eclipse.equinox.p2.metadata.repository"; bundle-version="1.2.200.v20150428-1613"; version="0.0.0"; x-friends:="org.eclipse.equinox.p2.engine"
                     org.eclipse.equinox.p2.metadata.repository [337]
                       Unresolved requirement: Import-Package: org.eclipse.equinox.internal.p2.persistence
                         -> Export-Package: org.eclipse.equinox.internal.p2.persistence; bundle-symbolic-name="org.eclipse.equinox.p2.repository"; bundle-version="2.3.100.v20150428-1613"; version="0.0.0"; x-friends:="org.eclipse.equinox.p2.artifact.repository,  org.eclipse.equinox.p2.engine,  org.eclipse.equinox.p2.metadata.repository,  org.eclipse.equinox.p2.ui.importexport,  org.eclipse.equinox.p2.repository.tools"
                            org.eclipse.equinox.p2.repository [342]
                              Unresolved requirement: Import-Package: org.eclipse.equinox.security.storage

    at org.eclipse.osgi.container.Module.start(Module.java:434)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1582)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1561)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.doContainerStartLevel(ModuleContainer.java:1533)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1476)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1)
    at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
    at      org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)

除了可重用性之外,还有任何速度优势,而不是在函数内移动正则表达式吗?像那样:

perl -ne 'BEGIN{$/ = \2769} print {open $f, ">",  ++$i; $f } $_' $DATAPATH/data/TRI001MSX.

我知道正在编译regexp以获得更好的性能。这是否意味着每次执行函数时最后一段代码片段都需要重新编译正则表达式?我猜大多数Javascript引擎都会缓存已编译的正则表达式。

我对V8 / Node.js特别感兴趣,但是关于其他引擎如何工作的一般知识也很有趣。

0 个答案:

没有答案