Skip to content

Commit e735a84

Browse files
author
Arpith Siromoney
committed
Optimize: Only JSON.stringify articles when storing to S3
Move JSON.stringify call inside the conditional that checks if article needs to be stored. This prevents stringifying every article in large feeds when most are already in Redis. Fixes memory leak that was causing 'JavaScript heap out of memory' crashes.
1 parent bee9d72 commit e735a84

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/feeds.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ const feed = {
194194
article.score = score(article);
195195
article.feedurl = feedURI;
196196

197-
const body = JSON.stringify(article);
198197
const key = article.hash;
199198
const rank = article.score;
200199
const articleKey = `article:${key}`;
@@ -213,6 +212,8 @@ const feed = {
213212
articleAddErr.log = zaddErr.message;
214213
stream.emit('error', articleAddErr);
215214
} else if ((oldscore === null) || (rank !== parseInt(oldscore))) {
215+
// Only stringify when we actually need to store it
216+
const body = JSON.stringify(article);
216217
s3.putObject({
217218
Key: key,
218219
Body: body,

0 commit comments

Comments
 (0)