Coder Social home page Coder Social logo

nodejs-vs-rust-vs-golang's Introduction

fibonacci 运行效率对比

nodejs 运行的代码:

function fibonacci(n) {
  if (n == 0 || n == 1) return n;
  return fibonacci(n - 1) + fibonacci(n - 2);
}

rust 运行的代码:

fn fibonacci(n: i32) -> i32 {
    if n == 0 || n == 1 {
        return n;
    }
    return fibonacci(n - 1) + fibonacci(n - 2);
}

go 运行的代码:

func fibonacci(n int32) int32 {
	if n == 0 || n == 1 {
		return n
	}
	return fib(n-1) + fib(n-2)
}

以上代码执行 fibonacci(42) (运算 8.66988873 亿次)的开销:

环境 数据类型 平均耗时
nodejs number(相当于 float64) 3.32s~3.903s
golang int32 3.533771625s
golang float64 5.038523542s
rust i32 0.894s
rust f64 2.052s
rust N-API(node 中调用 rust) i32 0.888013s
rust N-API(node 中调用 rust) f64 2.048s

结论,仅仅是简单计算,nodejs 竟然 和 golang 没什么区别, nodejs 的运算时间经常有浮动,不如另两个稳定. 可以看出,rust 编译成 N-API,在 node 中调用,计算性能没有损失。

nodejs 中,为了防止阻塞,涉及 CPU 密集型的任务尽量使用 worker_threads 执行。

nodejs-vs-rust-vs-golang's People

Stargazers

wangkris avatar

Watchers

James Cloos avatar Pillar Liang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.