diff --git a/benchmark/os/loadavg.js b/benchmark/os/loadavg.js new file mode 100644 index 00000000000000..6e3c57ed44b777 --- /dev/null +++ b/benchmark/os/loadavg.js @@ -0,0 +1,17 @@ +'use strict'; + +const common = require('../common.js'); +const loadavg = require('os').loadavg; + +const bench = common.createBenchmark(main, { + n: [5e6] +}); + +function main(conf) { + const n = +conf.n; + + bench.start(); + for (var i = 0; i < n; ++i) + loadavg(); + bench.end(n); +} diff --git a/lib/os.js b/lib/os.js index 7ab0ec5fa423be..ad49f4d6f63afd 100644 --- a/lib/os.js +++ b/lib/os.js @@ -1,12 +1,12 @@ 'use strict'; const binding = process.binding('os'); +const getLoadAvg = binding.getLoadAvg; const constants = process.binding('constants').os; const internalUtil = require('internal/util'); const isWindows = process.platform === 'win32'; exports.hostname = binding.getHostname; -exports.loadavg = binding.getLoadAvg; exports.uptime = binding.getUptime; exports.freemem = binding.getFreeMem; exports.totalmem = binding.getTotalMem; @@ -17,6 +17,12 @@ exports.networkInterfaces = binding.getInterfaceAddresses; exports.homedir = binding.getHomeDirectory; exports.userInfo = binding.getUserInfo; +const avgValues = new Float64Array(3); +exports.loadavg = function loadavg() { + getLoadAvg(avgValues); + return [avgValues[0], avgValues[1], avgValues[2]]; +}; + Object.defineProperty(exports, 'constants', { configurable: false, enumerable: true, diff --git a/src/node_os.cc b/src/node_os.cc index 97b1a1d08da6ed..211ac3d01dd8b2 100644 --- a/src/node_os.cc +++ b/src/node_os.cc @@ -28,8 +28,10 @@ namespace node { namespace os { using v8::Array; +using v8::ArrayBuffer; using v8::Boolean; using v8::Context; +using v8::Float64Array; using v8::FunctionCallbackInfo; using v8::Integer; using v8::Local; @@ -182,14 +184,12 @@ static void GetUptime(const FunctionCallbackInfo& args) { static void GetLoadAvg(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); - double loadavg[3]; + CHECK(args[0]->IsFloat64Array()); + Local array = args[0].As(); + CHECK_EQ(array->Length(), 3); + Local ab = array->Buffer(); + double* loadavg = static_cast(ab->GetContents().Data()); uv_loadavg(loadavg); - Local loads = Array::New(env->isolate(), 3); - loads->Set(0, Number::New(env->isolate(), loadavg[0])); - loads->Set(1, Number::New(env->isolate(), loadavg[1])); - loads->Set(2, Number::New(env->isolate(), loadavg[2])); - args.GetReturnValue().Set(loads); }