├── .gitignore ├── binding.gyp ├── src └── hello.cc ├── README.md ├── main.js ├── package.json └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | a.out 2 | build/ 3 | node_modules/ 4 | .vscode/ 5 | -------------------------------------------------------------------------------- /binding.gyp: -------------------------------------------------------------------------------- 1 | { 2 | "targets": [ 3 | { 4 | "target_name": "hello", 5 | "sources": [ "src/hello.cc" ], 6 | "include_dirs": [" 2 | #include 3 | using namespace v8; 4 | 5 | NAN_METHOD(hello) { 6 | int count = 0; 7 | for (int i = 0; i < 1000000; i++) { 8 | count = i; 9 | } 10 | 11 | std::cout << "hello\n"; 12 | } 13 | 14 | NAN_MODULE_INIT(init) { 15 | Nan::SetMethod(target, "hello", hello); 16 | } 17 | 18 | NODE_MODULE(hello, init); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fast-js 2 | Example of how to start writing c++ modules for node 3 | 4 | 1. make sure your have node 9.8.0 installed 5 | 2. `$ npm install` 6 | 3. run the project with `$ npm start` 7 | 8 | ## What does it do? 9 | The example here is comparing the speed a which we do the same operations in c++ as also in JavaScript. 10 | 11 | The operations include the following: 12 | - iterate for a large number count 13 | - changing a value of an number/integer on the program stack 14 | - printing a string of "hello\n" to standard output (stdout) 15 | -------------------------------------------------------------------------------- /main.js: -------------------------------------------------------------------------------- 1 | require('nan') 2 | console.log("finished \"nan\""); 3 | const { hello } = require('./build/Release/hello.node'); 4 | console.log("finished \"hello\""); 5 | 6 | const cPlusPlusHelloTime = "c++ hello" 7 | console.time(cPlusPlusHelloTime); 8 | hello(); 9 | console.timeEnd(cPlusPlusHelloTime); 10 | 11 | const jsLoopTime = "js loop"; 12 | console.time(jsLoopTime); 13 | let count = 0; 14 | for (let i = 0; i < 1000000; i++) { 15 | count = i; 16 | } 17 | console.log("hello\n"); 18 | console.timeEnd(jsLoopTime); 19 | 20 | console.log("\"hello\" function called"); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fast-js", 3 | "version": "1.0.0", 4 | "description": "Example of how to start writing c++ modules for node", 5 | "main": "main.js", 6 | "scripts": { 7 | "start": "node main.js", 8 | "rebuild": "node-gyp rebuild" 9 | }, 10 | "dependencies": { 11 | "node-gyp": "^5.0.1", 12 | "nan": "^2.14.0" 13 | }, 14 | "engines": { 15 | "node": "9.8.0" 16 | }, 17 | "author": "Ricky Powell ", 18 | "repository": "https://github.com/rickypowell/fast-js.git", 19 | "license": "ISC", 20 | "gypfile": true 21 | } 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Ricky 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | --------------------------------------------------------------------------------