Skip to content

Commit

Permalink
Improve performance for res.json/res.jsonp in most cases
Browse files Browse the repository at this point in the history
closes #2900
  • Loading branch information
bnjmnt4n authored and dougwilson committed May 31, 2016
1 parent b69b760 commit f90f9dd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
==========

* Improve performance for `res.json`/`res.jsonp` in most cases
* deps: accepts@~1.3.3
- Fix including type extensions in parameters in `Accept` parsing
- Fix parsing `Accept` parameters with quoted equals
Expand Down
17 changes: 15 additions & 2 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ res.json = function json(obj) {
var app = this.app;
var replacer = app.get('json replacer');
var spaces = app.get('json spaces');
var body = JSON.stringify(val, replacer, spaces);
var body = stringify(val, replacer, spaces);

// content-type
if (!this.get('Content-Type')) {
Expand Down Expand Up @@ -281,7 +281,7 @@ res.jsonp = function jsonp(obj) {
var app = this.app;
var replacer = app.get('json replacer');
var spaces = app.get('json spaces');
var body = JSON.stringify(val, replacer, spaces);
var body = stringify(val, replacer, spaces);
var callback = this.req.query[app.get('jsonp callback name')];

// content-type
Expand Down Expand Up @@ -1051,3 +1051,16 @@ function sendfile(res, file, options, callback) {
// pipe
file.pipe(res);
}

/**
* Stringify JSON, like JSON.stringify, but v8 optimized.
* @private
*/

function stringify(value, replacer, spaces) {
// v8 checks arguments.length for optimizing simple call
// https://bugs.chromium.org/p/v8/issues/detail?id=4730
return replacer || spaces
? JSON.stringify(value, replacer, spaces)
: JSON.stringify(value);
}

0 comments on commit f90f9dd

Please sign in to comment.