C layer • Callback receives status object, e.g. { code: 1 } • Application expected to check status code Client.prototype.get = function (key, policy, callback) { if (typeof policy === 'function') { callback = policy policy = null } else if (typeof callback !== 'function') { throw new TypeError('"callback" must be a function') } this.as_client.get(key, policy, callback) } client.get(key, function (status, record) { if (status.code != 0) { throw new Error(status.message) } // process record })
“despised”[1] Array constructor • Iterating through arguments • ES6: Use the new spread operator • Or is there a better, naive solution? [1] MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
&& err.code !== 0) return cb(err) var len = arguments.length var args = [null] for (var i = 2; i < len; i++) { args.push(arguments[i]) } cb.apply(undefined, args) }
'optimized' case 2: return 'not optimized' case 3: return 'always optimized' case 4: return 'never optimized' case 6: return 'maybe deoptimized' case 7: return 'optimized by TurboFan' default: 'unknown' } } function printStatus(f) { console.log('# Function %s is %s', f.name, status(f)) }
--allow-natives-syntax optimize.js | grep "^#" # Function cbSliceArguments is not optimized # Function cbArrayConstructor is optimized # Function cbIterateArguments is optimized # Function cbIterateArgumentsUsingPush is not optimized # Function cbSpreadOperator is not optimized # Function cbNaive is optimized