Narwhal 和 Node.js 之间的差异

发布于 2024-09-26 21:28:24 字数 1432 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

绮烟 2024-10-03 21:28:24
  1. 如果您使用 Node.js 或 Narwhal,请仅使用与各自引擎兼容的包和模块。目前,编写在这两个引擎上运行的应用程序、包和模块存在许多细微差别。来自 Dojo 的 Kris Zyp 付出了相当大的努力来使他的软件包在两个系统上都能运行,我想不出还有其他人。

  2. Narwhal 的输入和输出模块是阻塞的,很像 Python、Ruby、Perl、C、Java 等的标准库。

    但是,有一类应用程序无法使用阻塞 I/O 进行有效编写,例如在服务器内存中维护其状态的游戏以及与众多客户端进行状态通信的游戏。只有实验才能揭示线程或事件循环对于单个应用程序是否性能更好。但是,在大多数编程语言和库生态系统中编写“事件”应用程序更加困难和危险,因为使用任何阻塞 I/O 和阻塞 I/O 可以很快消除使用非阻塞 I/O 的好处。 O 经常隐藏在体系结构的各层中,甚至低至操作系统接口。 Node.js 令人兴奋,因为它正在创建一个具有严格异步 I/O 的生态系统,这使其成为第一个相当容易编写此类应用程序的系统。

    Douglas Crockford 和 Mark Miller 这样的支持者认为异步事件循环编程是<大多数应用程序的编写应该是因为更容易推理这些系统中的数据流、并发性和安全性,并在不影响正确性或完整性的情况下盲目地构建此类子系统。

    但是,如果您想利用 JavaScript 作为一种语言,但又不想承担事件循环编程的额外复杂性,Narwhal 被设计为可以在 JavaScriptCore(背后的快速 JavaScript 引擎)上运行 Safari,以及 Rhino。使用 Rhino,您可以访问 Google App Engine。 Narwhal 旨在为您提供 JavaScript 引擎的灵活性,但它没有考虑 Node.js 的 I/O 模型。 Narwhal 还被 280 North 软件生态系统广泛使用,用于构建 Cappuccino Objective-J 应用程序,例如 杰克和杰克。

  3. Node.js 和 Narwhal 都可以用于一般应用程序和 Web 服务器。 Node.js 特别适合网络客户端和服务器。 Narwhal 特别适合 Unix 风格的程序和 JSGI、类似 CGI 的 Web 服务器,并且被设计为在各种 Web 服务器上运行 JSGI 应用程序而无需更改。

    编写同时在 Narwhal 和 Node.js 上运行的应用程序很困难,但也是可能的。编写适用于 Narwhal 和 Node.js 的“包”是可能的,但必须谨慎行事。如果一个包没有宣传它是在 Narwhal 和 Node.js 上设计和测试的,那么你可以打赌它只能在其中之一上运行。

    io:不使用 I/O 子系统的模块(例如解析器、格式化程序、编码器和解码器)特别适合在 Narwhal 和 Node.js 之间共享代码。< /p>

    包:NPM(节点包管理器)和 Tusk(Narwhal 的包管理器)的包布局方式有所不同。它们都使用 package.json,但“依赖项”各自具有不同的含义。独角鲸即将推出一个补丁,使其能够容忍这种不一致。当软件包安装在 Narwhal 中时,它们都共享相同的模块名称空间,就像 Ruby 一样。使用 NPM,每个包都有一个与包同名的模块名称空间子树。

    模块: Node.js 和 Narwhal 都为 CommonJS 提供了不同的扩展 模块规格

  4. Node.js 提供了额外的自由变量,例如 __dirname

  5. Node.js 允许使用 module.exports = x 重新分配导出对象。

  6. Narwhal 提供了 require.once(id, scope) 来执行一次模块(无论之前是否已加载),并在作用域中使用额外的自由变量(这些变量有时被错误地称为“全局变量”) )。

  7. Node.js 不提供 CommonJS module.path 为当前模块的文件名。

  8. Narwhal 和 Node.js 提供了不兼容的系统来扩展模块加载器以处理模块的替代语言,例如 CoffeeScript 和 Objective-J。

  1. If you're using either Node.js or Narwhal, only use packages and modules that advertise compatibility with your respective engine. There are presently many nuances to writing applications, packages, and modules that work on both engines. Kris Zyp from Dojo has put quite a bit of effort into making his packages work on both systems and I cannot think of anyone else.

  2. Narwhal's input and output modules are blocking, much like the standard libraries for Python, Ruby, Perl, C, Java, and so on.

    There is, however, a class of applications that cannot be effectively written with blocking I/O, like games that maintain their state in the memory of the server and stateful communication with numerous clients. Only experimentation can reveal whether threads or event loops perform better for individual applications. But, it is furthermore difficult and perilous to write "evented" applications in most programming languages and library ecosystems because the benefits of using non-blocking I/O can be quickly obviated by making use of any blocking I/O, and blocking I/O is frequently hidden in the layers of architecture, even as low as the operating system interface. Node.js is exciting, because it is creating an ecosystem with strictly asynchronous I/O, which makes it the first system in which this class of application is reasonably easy to write.

    Proponents like Douglas Crockford and Mark Miller argue that asynchronous event loop programming is the way most applications should be written because it is easier to reason about data flow, concurrency, and security in these systems and to blindly compose such subsystems without compromising correctness or integrity.

    However, if you want to take advantage of JavaScript as a language, but do not want to buy into the additional complexity of event-loop programming, Narwhal is designed to work on both JavaScriptCore, the fast JavaScript engine behind Safari, and also on Rhino. Using Rhino gives you access to Google App Engine. Narwhal was designed to give you flexibility of your JavaScript engine, but it did not account for Node.js's I/O model. Narwhal is also used extensively by the 280 North software ecosystem, for build tools and servers for Cappuccino Objective-J applications, like Jake and Jack.

  3. Both Node.js and Narwhal can be used for general applications and web servers. Node.js is particularly well-suited for network clients and servers. Narwhal is particularly well suited for Unix-style programs and JSGI, CGI-like web servers, and is designed to run JSGI applications on a variety of web servers without alteration.

    Writing applications that work on both Narwhal and Node.js is difficult but possible. Writing "packages" that work for Narwhal and Node.js is possible, but it must be done deliberately. If a package does not advertise that it's been designed and tested on both Narwhal and Node.js, you can bet it will only work on one or the other.

    io: Modules that do not make use of I/O subsystems, like parsers, formatters, encoders, and decoders, are particularly well suited for code sharing between both Narwhal and Node.js.

    packages: There are differences in the way packages are laid out for NPM (Node Package Manager) and Tusk (Narwhal's package manager). They both use package.json, but "dependencies" have different meanings on each. There is an upcoming patch for Narwhal that allows it to tolerate this inconsistency. When packages are installed in Narwhal, they all share the same module name-space, like Ruby. With NPM, each package has a subtree of the module name space by the same name as the package.

    modules: Node.js and Narwhal both provide varying extensions to the CommonJS module specification.

  4. Node.js provides additional free variables like __dirname.

  5. Node.js allows the exports object to be reassigned with module.exports = x.

  6. Narwhal provides require.once(id, scope) for executing a module once (regardless of whether it has been previously loaded) with extra free variables in scope (these are sometimes erroneously called "globals").

  7. Node.js does not provide the CommonJS module.path for the file name of the current module.

  8. Narwhal and Node.js provide incompatible systems for extending the module loader to handle alternate languages for modules, like CoffeeScript and Objective-J.

烟若柳尘 2024-10-03 21:28:24

我只需添加 RingoJS 即可。它是一个基于Rhino的CommonJS系统,但与Narwhal相比它更加成熟(它的主要作者多年来一直在开发其前身 Helma),并且通过跟踪 Git 存储库的开发,RingoJS 似乎更加活跃。如今独角鲸的开发似乎有点缓慢。

I would just add RingoJS to the mix. It is a Rhino-based CommonJS system, but comparing to Narwhal it is much more mature (its primary author has been developing its predecessor Helma for years) and by following both Git repository's development, RingoJS seems to be much more active. Development of Narwhal seems to be kind of slow these days.

如果没有 2024-10-03 21:28:24

如果您更喜欢 Narhwal 的同步风格,您还可以使用我的 Common Node 包,它允许您在 Node.js 上运行同步 Narwhal、RingoJS 和其他 CommonJS 兼容包以及 JSGI Web 应用程序。

If you prefer Narhwal's synchronous style, you can also use my Common Node package which allows you to run synchronous Narwhal, RingoJS and other CommonJS compatible packages as well as JSGI web applications on Node.js.

昨迟人 2024-10-03 21:28:24

Node.js 不应该与 Narwhal 相比较。相反,它应该与 Rhino 进行比较。与 Rhino 一样,Node.js 是一个 JavaScript 解释器。

Node.js 符合 CommonJS 模块规范,因此所有库都与 CommonJS 兼容。看起来 Narwhal 也兼容 CommonJS,这意味着它们可以在 Node.js 中使用。

但首先看一下 Node.js 的标准模块,因为它似乎与 Narwhal 有很多重叠。另外,请查看可用于 Node.js 的第三方模块列表: http:// /github.com/ry/node/wiki/modules


附加答案:

啊,我现在明白了。 Narwhal 确实很像 Node.js。你说Narwhal 是一个让我失望的框架。我现在发现事实并非如此。事实上,介绍页面说您可以在 Narwhal 解释器之上运行像 Nitro 这样的框架。

Narwhal 和 Node.js 之间的区别基本上是 Narwhal 使用可插入的 JavaScript 引擎架构,而 Node.js 仅使用 V8。两者都是 JavaScript 的“shell”(我们暂时这样称呼它们,以避免与术语“解释器”混淆)。

我不确定为任一平台编写的 CommonJS 库在另一个平台上使用它能走多远。我猜肯定所有纯 JavaScript 库都是交叉兼容的。 Node.js 确实使用非阻塞 I/O 模型,因此 Narwhal 的某些二进制模块可能无法在 Node.js 上正常工作。

Node.js 确实强调回调风格的编程(以最大限度地利用非阻塞 I/O)。对于经验丰富的 JavaScript 程序员来说,这不是问题,因为我们习惯了 setTimeout()、XMLHttpRequest 等。事实上,作为经验丰富的 JavaScript 程序员,我更喜欢Node.js 的风格。 Narwhal 感觉太像 C 了。


示例:

这就是我所说的 Node 相对于 Narwhal 的不同“感觉”。

在 Narwhal 中,读取文件的示例是:

var fs = require("file");
var data = fs.read(myfilename); /* Code stops at this point
                                 * until all data is read
                                 */
/* Process data here */

在 Node.js 中是:

var fs = require('fs');
fs.readFile(myfilename, function(err,data) {
    /* Process data here */
});

/* readFile returns immediately and code continues
 * executing while file is being read
 */

就像 setTimeout 一样,Node.js 中读取文件是异步的(您的代码需要等待硬盘寻道并读取文件)读取数据,在此期间您可以运行其他代码)。

Node.js should not be compared to Narwhal. Instead it should be compared to Rhino. Like Rhino, Node.js is a JavaScript interpreter.

Node.js conforms to the CommonJS specification for modules, so all libraries for it are CommonJS compatible. It looks like Narwhal is also CommonJS compatible which would mean that they would be usable in Node.js.

But first look at Node.js's standard modules since there seems to be a lot of overlap there with Narwhal. Also, have a look at the list of third-party modules available for Node.js: http://github.com/ry/node/wiki/modules


Additional answer:

Ah, I see now. Narwhal is indeed like Node.js. You said that Narwhal is a framework which threw me off. I see now that it is not. Indeed, the intro page says that you can run frameworks like Nitro on top of the Narwhal interpreter.

The difference between Narwhal and Node.js is basically Narwhal uses a pluggable JavaScript engine architecture while Node.js just uses V8. Both are JavaScript "shells" proper (let’s call them that for now to avoid confusion with the term "interpreter").

I'm not sure how far one can take CommonJS libraries written for either platform and use it on the other platform. I would guess certainly all the pure-JavaScript libraries are cross compatible. Node.js does use a nonblocking I/O model, so some binary modules for Narwhal may not work correctly on Node.js.

Node.js does stress callback style programming though (to make maximum use of nonblocking I/O). For a seasoned JavaScript programmer this is not an issue since we're used to setTimeout(), XMLHttpRequest etc. In fact, as a seasoned JavaScript programmer, I sort of prefer Node.js's style. Narwhal feels too much like C.


Examples:

Here's what I mean by the different "feel" of Node over Narwhal.

In Narwhal, the example for slurping a file is:

var fs = require("file");
var data = fs.read(myfilename); /* Code stops at this point
                                 * until all data is read
                                 */
/* Process data here */

In Node.js it is:

var fs = require('fs');
fs.readFile(myfilename, function(err,data) {
    /* Process data here */
});

/* readFile returns immediately and code continues
 * executing while file is being read
 */

Just like setTimeout, reading files in Node.js is asynchronous (your code need to wait for the hard disk to seek and read data during which time you can run other pieces of code).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文