[nodejs]will kilos setTimeout spend too much resourece?

65 views
Skip to first unread message

jason.桂林

unread,
Apr 23, 2012, 1:49:08 AM4/23/12
to nod...@googlegroups.com
I need to write a service use something like in memory cache, and I want to do `expire` things in setTimeout, will it very slow, If I expire too much things, about kilos to millions.

--
Best regards,

Jason Green
桂林


Joran Greef

unread,
Apr 23, 2012, 2:32:34 AM4/23/12
to nod...@googlegroups.com
If you pass a function reference to an existing function to setTimeout, and then call this function repeatedly, rather than creating a closure each setTimeout call, that will lower your memory and cpu overhead substantially.

jason.桂林

unread,
Apr 23, 2012, 5:41:07 AM4/23/12
to nod...@googlegroups.com
Oh cool! and I found

setTimeout(console.log, 1000, 'hello world')

is much better than

setTimeout(function(){
   console.log('hello world');
}, 1000)

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

alFReD NSH

unread,
Apr 24, 2012, 10:30:49 PM4/24/12
to nod...@googlegroups.com
I think for something like that you should decide how accurate you want to be and use a function on an interval to remove all expired things. Like run the function on every minute or every second, depending on your need. A  A lot of timers are not a good, and not that much accurate.

jason.桂林

unread,
Apr 26, 2012, 2:57:27 AM4/26/12
to nod...@googlegroups.com
Reasonable !

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
Reply all
Reply to author
Forward
0 new messages