Jump to content
  • Advertisement
TufanMeric

Should I use Java or Node.js for my game server?

Recommended Posts

I've built half the game server with node.js, should I switch to java before it's too late? My game has some turrets that attack players in their range using the code below.

inRange = (x1, y1, x2, y2, w, h) => x1 < x2 + w && x1 > x2 - w && y1 < y2 + h && y1 > y2 - h;
for (let i = 0; i < this.room.entities.length; i += 1) {
      if (
        inRange(
          entity.body.position[0],
          entity.body.position[1],
          this.body.position[0],
          this.body.position[1],
          500,
          500,
        )
      ) {
			...attack
     	}
    }

I think this might not be the best way to do that in Node.js when there's 150-200 turrets since Node.js is single-threaded, which makes me think about rewriting the server in Java.

I'm currently using p2.js for physics, I will probably use dyn4j or JBox2D on Java.

Share this post


Link to post
Share on other sites
Advertisement

The JavaScript engine in node.js is reasonably high performance. 200 turrets updating 60 times a second should be totally possible, assuming you don't do bad things like ray trace from every turret to every target every frame. (Which would probably also kill a Java program.)

In general, Java is not THAT much faster than JavaScript. There really are only three "groups" of programming languages:

- statically compiled, C-like: C, C++, assembler, perhaps Rust, and some oddballs that almost nobody uses (Pascal, FORTRAN, D, etc)

- high-quality JIT languages: Java, JavaScript, C#, Haskell, go, etc

- dynamic languages with various design defects preventing any real performance: Python, PHP, Ruby, Lua, LISP, etc

Java-like languages are typically between 50% and 100% the speed of C/C++

Dynamic languages are typically between 8% and 40% the speed of C/C++

Of course, you can come up with some benchmark to "prove" any point you want, but over a large number of libraries and programs, for typical code written by typical programmers who know each of the languages, this is what my experience has been.

You may wonder: How come JavaScript is with Java/C#, and not with Python? It used to be a Python-like language in performance, but the billions of web-browsers on the world have driven every browser and JavaScript vendor to pour thousands of man-years into optimizing the JIT/runtime for the JavaScript interpreter, to the point where it's now almost as fast as Java and friends, for typical workloads, again depending on how you write the program.

If the reason to go with Java instead of JavaScript is to get better type checking, perhaps you can check out TypeScript instead?

Share this post


Link to post
Share on other sites
5 hours ago, hplus0603 said:

The JavaScript engine in node.js is reasonably high performance. 200 turrets updating 60 times a second should be totally possible, assuming you don't do bad things like ray trace from every turret to every target every frame. (Which would probably also kill a Java program.)

In general, Java is not THAT much faster than JavaScript. There really are only three "groups" of programming languages:

- statically compiled, C-like: C, C++, assembler, perhaps Rust, and some oddballs that almost nobody uses (Pascal, FORTRAN, D, etc)

- high-quality JIT languages: Java, JavaScript, C#, Haskell, go, etc

- dynamic languages with various design defects preventing any real performance: Python, PHP, Ruby, Lua, LISP, etc

Java-like languages are typically between 50% and 100% the speed of C/C++

Dynamic languages are typically between 8% and 40% the speed of C/C++

Of course, you can come up with some benchmark to "prove" any point you want, but over a large number of libraries and programs, for typical code written by typical programmers who know each of the languages, this is what my experience has been.

You may wonder: How come JavaScript is with Java/C#, and not with Python? It used to be a Python-like language in performance, but the billions of web-browsers on the world have driven every browser and JavaScript vendor to pour thousands of man-years into optimizing the JIT/runtime for the JavaScript interpreter, to the point where it's now almost as fast as Java and friends, for typical workloads, again depending on how you write the program.

If the reason to go with Java instead of JavaScript is to get better type checking, perhaps you can check out TypeScript instead?

Thanks for your time and detailed explanation, that helped and saved me a lot of time rewriting things in Java.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!