map方法的原生实现如此之快?

时间:2018-01-06 03:01:22

标签: javascript performance dictionary testing

map方法的原生实现如此之快?

我重新实现了它,它仍然比调用迭代数组的CMake-GUI方法慢大约2倍。

这是我的代码:

The Fortran compiler identification is GNU 7.2.0
The C compiler identification is GNU 7.2.0
The CXX compiler identification is GNU 7.2.0
Check for working Fortran compiler: C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/gfortran.exe
Check for working Fortran compiler: C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/gfortran.exe  -- works
Detecting Fortran compiler ABI info
Detecting Fortran compiler ABI info - done
Checking whether C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/gfortran.exe supports Fortran 90
Checking whether C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/gfortran.exe supports Fortran 90 -- yes
Check for working C compiler: C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/gcc.exe
Check for working C compiler: C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/gcc.exe -- works
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Detecting C compile features
Detecting C compile features - done
Check for working CXX compiler: C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/g++.exe
Check for working CXX compiler: C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/g++.exe -- works
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Detecting CXX compile features
Detecting CXX compile features - done
CMake Deprecation Warning at CMakeLists.txt:49 (CMAKE_POLICY):
  The OLD behavior for policy CMP0022 will be removed from a future version
  of CMake.

  The cmake-policies(7) manual explains that the OLD behaviors of all
  policies are deprecated and that a policy should be set to OLD only under
  specific short-term circumstances.  Projects should be ported to the NEW
  behavior and not rely on setting a policy to OLD.


Checking whether GFortran version >= 4.8 -- yes
Found MPI_C: C:/Windows/System32/msmpi.dll  
Found MPI_CXX: C:/Windows/System32/msmpi.dll  
Found MPI_Fortran: C:/Windows/System32/msmpi.dll  
Looking for pthread.h
Looking for pthread.h - found
Looking for pthread_create
Looking for pthread_create - found
Found Threads: TRUE  
CMake Error at C:/Program Files/CMake/share/cmake-3.10/Modules/FindBLAS.cmake:699 (message):
  A required library with BLAS API not found.  Please specify library
  location.
Call Stack (most recent call first):
  CMakeLists.txt:151 (find_package)


Configuring incomplete, errors occurred!
See also "F:/FreshElmerCM10MGW_ouput_x02/elmerfem/CMakeFiles/CMakeOutput.log".

我正在测试以下代码:

.map

为了进行比较,我创建了另一种基于“官方”MDN polyfill(https://mzl.la/2EeddOh)的方法。结果如下:

Array.prototype.toMap = function(cb, ctx) {
  if(typeof cb != 'function') {
    return
  }

  var
    result = [],
    len = this.length

  for(var i = 0; i < len; i++) {
    result.push(cb.call(ctx, this[i], i, this))
  }

  return result
}

你有什么想法?

1 个答案:

答案 0 :(得分:0)

正如评论中所讨论的那样,答案是非常频繁使用的功能可以用更接近硬件的语言实现,以提供更高的性能。

对于Node.js,具体来说,这是在V8引擎中,V8直接在C ++中实现Array.prototype.maphttps://github.com/v8/v8/blob/c38cb367e76ee8bd87719dd3699254e74f8ebfd1/src/builtins/builtins-array-gen.cc#L2536