node.js native binding slower than php native extension - node.js

Hello i am currently trying to port a php-extension (native C) to a node.js native extensions.
i am using nan ->https://github.com/rvagg/nan - for api abstraction
i have the following node cpp code:
NAN_METHOD(Core::getInfo) {
NanScope();
int somevar=1;
Local<Object> return_obj = NanNew<Object>();
return_obj->Set(NanNew<v8::String>("int"),NanNew<v8::Integer>(somevar));
return_obj->Set(NanNew<v8::String>("version"),NanNew<v8::String>("teststr"));
NanReturnValue(return_obj);
}
and the following php-extension c code:
PHP_FUNCTION(core_get_info) {
int somevar;
if (array_init(return_value) == FAILURE) {
RETURN_FALSE;
}
add_assoc_long(return_value, "int", somevar);
add_assoc_string(return_value, "version", "teststr", 1);
}
when running the call to this function in 1000000 iterations the NODE variation is MUCH Slower.
test.php
for($x=0; $x<1000000; $x++) {
$i = core_get_info();
}
var_dump($i);
test.js
var x=0;
var i;
for(x=0; x<1000000; x++) {
i=core.getInfo();
}
console.log(i);
the factor seems to be about 170% slower in node than in php - has any one a tipp what i am doing fundamentally wrong in the node part.
edit: node version v0.11.14

Related

Typescript with node.js giving "is not a constructor" error

I have a node.js application with two typescript files.
matchmanager.ts is defined as -
namespace LobbyService
{
export class MatchManager
{
constructor() { /*code*/ }
}
}
and main.ts which is defined as
namespace LobbyService
{
let matchManager: MatchManager = new MatchManager() ;
/* code */
}
I setup visual studio to output the files into a single JS file called lobbyservice.js
However, when i type
node lobbyservice.js
I get the following error -
TypeError: LobbyService.MatchManager is not a constructor
The generated file has this output -
var LobbyService;
(function (LobbyService) {
var matchManager = new LobbyService.MatchManager();
})(LobbyService || (LobbyService = {}));
var LobbyService;
(function (LobbyService) {
var MatchManager = (function () {
function MatchManager() {
console.log("created");
}
return MatchManager;
}());
LobbyService.MatchManager = MatchManager;
})(LobbyService || (LobbyService = {}));
This was working before, but for some odd reason it isn't now. Any thoughts?
Update - I managed to get a version of the lobbyservice.js that works. For some odd reason, Visual studio transforms one version of the file into the one above, and one into this -
var LobbyService;
(function (LobbyService) {
var MatchManager = (function () {
function MatchManager() {
console.log("created");
}
return MatchManager;
}());
LobbyService.MatchManager = MatchManager;
})(LobbyService || (LobbyService = {}));
var LobbyService;
(function (LobbyService) {
var matchManager = new LobbyService.MatchManager();
})(LobbyService || (LobbyService = {}));
//# sourceMappingURL=lobby.js.map
No clue as to why i'm getting two different outputs like that for the same source code. Both projects have the same module property of "none"
So user Elliott highlighted that indeed it's a know typescript compile quirk where the order of the output javascript file creates an issue.
to fix that, i had to add
/// <reference path="matchmanager.ts"/>
On my typescript files that used MatchManager class, even though they were on the same namespace and compiled ok. This forced the typescript compiler to create a workable javascript output.

Making ESLint work for files which are loaded by others

I have a set of scripts in which one loads another script file and uses the functions defined in it.
For example, let's say I have main.js script with the following source
load("helper.js");
var stdin = new java.io.BufferedReader( new java.io.InputStreamReader(java.lang.System['in']) );
function readline() {
var line = stdin.readLine();
return line;
}
var N = parseInt(readline());
for(var i = 0; i< N; i++)
{
print("fd630b881935b5d43180ff301525488a");
var num = parseInt(readline());
var ans = perfectNumberCheck(num);
print(ans);
print("dc29e6fa38016b00627b6e52956f3c64");
}
I have another script file, helper.js which has the following source
function perfectNumberCheck(num) {
if(num == 1)
{
return 0;
}
var halfNum = (num/2) + 1;
var sum = 0;
var retVal = 0;
for(var i=1 ; i < halfNum; i++){
if(num % i === 0){
sum = sum + i;
}
}
if(sum == num){
retVal = 1;
}
else {
retVal = 0;
}
return retVal;
}
As it can be seen, main.js uses the function perfectNumberCheck. Now, when I run ESLint on both the files using eslint main.js helper.js or by using eslint *.js, I get the no-unused-vars error 'perfectNumberCheck' is defined but never used even though it is being used in main.js.
I want to keep this error in the configuration but don't want ESLint to show it in such cases.
Is there a way to make ESLint resolve these dependencies without writing the entire code in a single script file?
You can add a comment like /* exported perfectNumberCheck */ to helper.js to tell no-unused-vars that the function is used elsewhere outside that file:
/* exported perfectNumberCheck */
function perfectNumberCheck() {
// ...
}
By itself, ESLint lints each file in isolation, so it will not resolve identifiers defined in other files. The /* exported ... */ comment and globals allow you to inform ESLint of dependencies in other files.
Since exported comments are intended to be used to indicate names that are used elsewhere by being present in the global scope, they have no effect when the file's source in not in the global scope. Specifically, quoting the docs:
Note that /* exported */ has no effect for any of the following:
when the environment is node or commonjs
when parserOptions.sourceType is module
when ecmaFeatures.globalReturn is true

Node.js ecmascript 6 mocking class methods

Can node.js ecmascript 6 features be used for mocking function calls ? I see there is Proxy but not sure how to use it.
Let say.. I have a code
class E{
someOtherMethod(a){
return 100;
}
}
class D{
someMethod(a){
// some ... code
var e = new E();
e.someOtherMethod();
// some ... more code
}
}
I want to mock it to when i have reference of just D and instance of E is created within someMethod, that i want to test.
var d = new D();
when(E).someOtherMethod(a).return(1); // Or anything similar with ES 6
Just try it out.
It should work mind you. ES6 doesn't change the internals of Javascript, it just puts some syntactic sugar on top of it. Here is the code I just tested out in Chrome (and it works by returning 'Mock'), so it should work fine without the function wrapper in Node.
(function() {
'use strict';
class D {
someMethod() {
return 100;
}
};
var obj = new D();
var oldMthd = obj.someMethod;
obj.someMethod = function() { return 'Mock'; }
alert(obj.someMethod());
})();

Generator functions in node 0.12

Are generator functions supported in Node? I keep reading that they are since v 0.11.
Tried to run this snippet in node 0.12
function* range(max, step) {
var count = 0;
step = step || 1;
for (var i = 0; i < max; i += step) {
count++;
yield i;
}
return count;
}
but no luck. Do I need to call npm start with a custom parameter?
You need to run node with --harmony flag to enable generators in node (0.11.x and 0.12.x versions):
$ node --harmony your_script.js

How do I require a minimum version of node.js in my script?

I just found out that a script I wrote only works on node 0.10 because it uses readable events.
How do I require a minimum version of node.js in my script so that users know that they need to upgrade?
In package.json:
{ "engines" : { "node" : ">=0.10.3" } }
From the docs.
Edit, a programmatic way:
var pkg = require('./pacakge'),
semver = require('semver');
if(!semver.satisfies(process.version, pkg.engines.node)) {
// Not sure if throw or process.exit is best.
throw new Error('Requires a node version matching ' + pkg.engines.node);
}
Add this to top the top of your script.
var versionComps = process.versions['node'].split('.');
if (parseInt(versionComps[0]) === 0 && parseInt(versionComps[1]) < 10) {
console.log('Script requires node.js version >= 0.10');
process.exit(1);
};

Resources