- vừa được xem lúc

Blog#184: 🥳NodeJS 18 is HERE! 3 Mind-Blowing Features You Need to Know🏎️💨

0 0 13

Người đăng: NGUYỄN ANH TUẤN

Theo Viblo Asia

184

Hi, I'm Tuan, a Full-stack Web Developer from Tokyo 😊. Follow my blog to not miss out on useful and interesting articles in the future.

😊Meet the NodeJS 18, bringing exciting new features to make your development experience even better! With the Standalone Test Runner, you can now test your code natively, making your life easier. Fetch Experimental Support introduces the much-loved fetch function to NodeJS, while the upgraded V8 Engine 10.1 offers enhanced performance and capabilities. Embrace NodeJS 18 and enjoy the friendly boost to your coding! 🌟

1. Standalone Test Runner

Testing is crucial for managing the risk of errors in production systems. Typically, developers use popular libraries like Jest, AVA, Supertest, and MochaJS to write tests in NodeJS. However, starting with NodeJS 18, there's a native way to implement tests. You can use the new API like this:

import test from 'node:test';
import assert from 'node:assert'; test('synchronous passing test', (t) => { // This test passes because it does not throw an exception. assert.strictEqual(1, 1);
}); test('synchronous failing test', (t) => { // This test fails because it throws an exception. assert.strictEqual(1, 2);
});

You can also use the context of the test() function to create sub-tests:

import test from 'node:test';
import assert from 'node:assert'; test('top level test', async (t) => { await t.test('subtest 1', (t) => { assert.strictEqual(1, 1); }); await t.test('subtest 2', (t) => { assert.strictEqual(2, 2); });
});

Keep in mind that you need to use the node: prefix when importing the test module, or NodeJS will look for it in your node_modules as an external package.

2. Fetch Experimental Support

The community is excited about this feature because it allows developers to use the fetch function within NodeJS. In a browser context, you can use fetch to make requests, like this:

function getProducts() { fetch('https://api.site.com/api/v1/products') .then(response => response.json()) .then(data => console.log(data));
}

You can also use async/await mode:

async function getProducts() { const response = await fetch('https://api.site.com/api/v1/products'); const data = await response.json(); console.log(data);
}

These methods work in browsers but not in server-side contexts where you might want to make requests or connect to other services via fetch. With NodeJS 18's experimental support, you can now use fetch in NodeJS:

const res = await fetch('https://api.site.com/api/v1/products');
if (res.ok) { const data = await res.json(); console.log(data);
}

This means you can make requests using fetch just like in browsers.

3. V8 Engine Version 10.1

NodeJS 18 supports version 10.1 of the V8 engine, which comes with several new features:

  • Added findLast and findLastIndex methods on arrays
  • More improvements in the Intl.Locale API
  • The Intl.supportedValuesOf function

image.png

The updated V8 JavaScript engine, version 10.1, originates from Chromium 101. With this update, Node.js 18 introduces the findLast() and findLastIndex() vector methods, the Intl.supportedValuesOf function, enhancements to the Intl.Locale API, and better performance for class fields and private class methods.

These improvements provide developers with more powerful tools and better performance for their NodeJS applications. As a result, you can expect faster processing, more efficient code execution, and overall improved performance in your projects using NodeJS 18.

Conclusion

In summary, NodeJS 18 brings essential features like the Standalone Test Runner, Fetch Experimental Support, and an updated V8 Engine to enhance productivity and performance. Start using these new features if you're running an existing application on NodeJS or looking to build one from scratch. As you embrace these new features, you'll experience the benefits of the advancements NodeJS 18 has to offer.

And Finally

As always, I hope you enjoyed this article and got something new. Thank you and see you in the next articles!

If you liked this article, please give me a like and subscribe to support me. Thank you. 😊

Ref

Bình luận

Bài viết tương tự

- vừa được xem lúc

Cài đặt WSL / WSL2 trên Windows 10 để code như trên Ubuntu

Sau vài ba năm mình chuyển qua code trên Ubuntu thì thật không thể phủ nhận rằng mình đã yêu em nó. Cá nhân mình sử dụng Ubuntu để code web thì thật là tuyệt vời.

0 0 374

- vừa được xem lúc

Hướng dẫn làm bot Facebook messenger cho tài khoản cá nhân

Giới thiệu. Trong bài viết trước thì mình có hướng dẫn các bạn làm chatbot facebook messenger cho fanpage. Hôm nay mình sẽ hướng dẫn các bạn tạo chatbot cho một tài khoản facebook cá nhân. Chuẩn bị.

0 0 146

- vừa được xem lúc

Crawl website sử dụng Node.js và Puppeteer - phần 2

trong phần 1 mình đã giới thiệu về puppeteer và tạo được 1 project cùng một số file đầu tiên để các bạn có thể crawl dữ liệu từ một trang web bất kỳ. Bài này mình sẽ tiếp nối bài viết trước để hoàn thiện seri này.

0 0 58

- vừa được xem lúc

Điều React luôn giữ kín trong tim

■ Mở đầu. Ngồi viết bài khi đang nghĩ vu vơ chuyện con gà hay quả trứng có trước, mình phân vân chưa biết sẽ chọn chủ đề gì để chúng ta có thể cùng nhau bàn luận.

0 0 42

- vừa được xem lúc

Gửi Mail với Nodejs và AWS SES

AWS SES. AWS SES là gì.

0 0 71

- vừa được xem lúc

Crawl website sử dụng Node.js và Puppeteer - phần 1

Bài viết này mình sẽ giới thiệu cho các bạn craw dữ liệu của web site sử dụng nodejs và Puppeteer. .

0 0 149