Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7795057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:57:20+00:00 2026-06-01T22:57:20+00:00

For the full story, check out my other question . Basically, I had asked

  • 0

For the full story, check out my other question.

Basically, I had asked if it were more efficient to use named functions in the socket handlers for the following code:

var app = require('express').createServer()
var io = require('socket.io').listen(app);

app.listen(8080);

// Some unrelated stuff

io.sockets.on('connection', function (socket) {
    socket.on('action1', function (data) {
        // logic for action1
    });

    socket.on('action2', function (data) {
        // logic for action2
    });

    socket.on('disconnect', function(){
        // logic for disconnect
    });
});

The overall answer was yes (see the above link for more details), but the following comment was posted by ThiefMaster:

I’m not familiar with V8 internals but it might be smart enough to compile the function once and re-use it everytime, just with a different scope attached.

So now that’s my question. Is V8 smart enough to compile anonymous functions once and reuse them with different scopes in situations where anonymous functions ordinarily lead to several function instances being created? For example, above I would expect the handler for the connection event to be created once but the handlers for action1, action2, and disconnect to be created for each connection. In the other question this was solved with named functions but I am more interested if this is necessary in V8 or if it will do some optimizations.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:57:22+00:00Added an answer on June 1, 2026 at 10:57 pm

    Yes. I asked a very similar question (related in my case to creating functions from within a constructor function) on the V8 mailing list. I got the reply that the function’s code is “…normally reused…”, even though there’s a separate function object each time (as required by the spec).


    Note, though, that your question has nothing to do with whether the function is named or anonymous. The function in your example could have a name:

    io.sockets.on('connection', function handleConnection(socket) {
        socket.on('action1', function (data) {
            // logic for action1
        });
    
        socket.on('action2', function (data) {
            // logic for action2
        });
    
        socket.on('disconnect', function(){
            // logic for disconnect
        });
    });
    

    That uses a named function expression, which is perfectly valid and handled correctly by V8. (Sadly, it’s not handled correctly by IE8 and earlier, which create two completely different functions at totally different times. But as you’re using V8, you don’t have to worry about that.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please clarify using a SIMPLE user story the full slice of what
The full story is I want to disable the submit button on a form's
I had a quick question regarding RegEx... I have a string that looks something
The full story is that I'm exporting a table within ms-access 2007 as an
I am using a socket library (I'd rather not not use it) whose recv
Full story: I am trying to start up an instance of hudson with a
Full disclosure : This is for a homework assignment. This is driving me nuts.
Full disclosure, I'm new to Visual Studio Web Tests and coding for them. I've
FULL EDIT: I urgently need to access a Microsoft SQL Server and read compressed
The full message that I got on the console is: Invalid memory access of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.