Giter VIP home page Giter VIP logo

node-addon-study's Introduction

Node.js C++ addon

官方文档

官方示例

基本概念

  • C++ addon 允许开发者调用 v8、libuv、Node.js 内部库、Node.js 外部依赖(如 OpenSSL)开发能够使用 require 动态加载的模块。
  • nan:屏蔽了 v8 的版本差异。
  • Node-API:提供了 ABI 的能力,即屏蔽了 Node.js 版本差异,满足指定 ABI 接口标准的二进制能够用于多种不同的 Node.js 版本。
  • node-addon-api:进一步封装 C++ 类,简化开发。

代码示例

  • 比较 JS 与 C++ addon 冒泡排序的性能。
  • JS 实现及比较逻辑参见 test.js
  • C++ addon 实现参见 sort.cc
    • 参考了 这篇文章,但可能存在的问题是:在 Napi::Arrayunsigned int* 之间做了两次数据拷贝以实现对 Napi::Array 的元素移动,这样是否会导致 性能下降
  • 比较结果:确有提升,但不明显,最多为 20%。
元素个数 JS 耗时 C++ addon 耗时
1000 3 1
10000 100 80
100000 12000 10000
500000 310000 270000

如何运行

首先,参考官方文档搭建 node-addon-api 环境。

npm install -g node-gyp
npm install
node-gyp configure build

build/Release/sort.node 拷贝至根目录。

node test.js

node-addon-study's People

Contributors

gstarp avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.