Saturday, March 25, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home JavaScript

Intro to Qwik: A superfast JavaScript framework

learningcode_x1mckf by learningcode_x1mckf
October 19, 2022
in JavaScript
0
Intro to Qwik: A superfast JavaScript framework
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


Qwik is a daring rethink of how reactive UIs work. The core premise is that the framework is constructed from the bottom as much as ship HTML with minimal JavaScript—simply sufficient JavaScript to incrementally introduce interactivity as wanted. 

Qwik makes use of a fine-grained mannequin for isolating the segments of the app which might be hydrated on an as-needed foundation. By ranging from first ideas, Qwik permits for in any other case unattainable efficiency and represents an alternate path for the evolution of front-end JavaScript.

State of Qwik

Qwik continues to be in early releases, however it has come a great distance since we first got a look at it.  There may be now a full-featured instance at StackBlitz, a REPL playground, and a command line device. Qwik has additionally developed to assist a extra developer-friendly, React-like syntax. Underneath the hood continues to be a complicated, one-of-a-kind reactive engine that defines reactive boundaries alongside state, templates, and listeners. 

Resumability

Qwik makes use of a mixture of intelligent server-side and client-side rendering to keep away from the type of double taxation that up to date frameworks endure in performing the hydration work twice, as soon as on the server, and once more on the shopper.

As Qwik creator Misko Hevery writes:

The fundamental concept behind Qwik is that it’s resumable. It might probably proceed the place the server left off. There may be however the tiniest quantity of code to execute on the shopper.

Or, put one other method: Let the server arrange an HTML web page as absolutely practical as attainable, and permit the shopper to carry out the smallest quantity of labor attainable to proceed, or resume, the method for the person.

The standard circulation in reactive frameworks with SSR (server-side rendering) is to first generate a model of the app on the server, then ship it over to the shopper, which renders the scaffolded app. At that time, the client-side app takes over, mainly having to bootstrap the identical app once more with the intention to wire collectively a functioning shopper. 

This course of is named hydration. There are a number of intelligent methods to attempt to make hydration extra environment friendly, however Qwik abandons these for a brand new course of known as resumability.

Resumability means the shopper can decide up the place the server left off, with out having to rebuild the app on the shopper. 

Time to interactive

The metric that Qwik strives to boost is time to interactive (TTI). This refers back to the period of time that elapses between when the person makes a request to an internet web page and when the web page turns into aware of the person’s interplay.

Whereas time to load (TTL) tracks how lengthy it takes for the shopper to complete receiving all required information (and subsequently is a metric decided largely by file sizes and community velocity), TTI takes into consideration a distinguished truth of contemporary JS frameworks: As soon as the information is downloaded, the shopper should then unpack and execute the JavaScript code required to make the web page interactive.

There may be loads of work that goes right into a reactive engine. The engine should unravel/parse all that markup (like JSX) shot by way of with variables and expressions that modify what’s displayed based mostly on altering state, and the way it behaves based mostly on the code.

On the different finish of the spectrum is a straight HTML web page. As soon as the browser has a maintain of it, the web page is able to rock. That is why Google’s PageSpeed Insights provides a web page like Reddit.com a 32 out of 100 rating, whereas uncooked HTML scores 100.

Closures and listeners

The technical hurdle to quick TTI is described by Hevery as “dying by closure.” Briefly, the truth that each closure should keep the enclosing universe of data means the runtime app is a layer cake of eagerly loaded code.

The answer employed by Qwik is to make use of a world occasion listener that interacts with serialized listeners. In different phrases, a common occasion listener is used to orchestrate listeners which might be realized on demand, as an alternative of listeners being downloaded and wrapped in closures (no matter whether or not they’ll ever truly execute).

Qwik goals to ship reactivity in-line with the HTML, making all of it serializable. Solely a small executable is required to then manifest the reactivity at runtime, based mostly on the data encapsulated within the markup.

Code splitting, finely tuned

One other method to have a look at that is that Qwik is performing fine-tuned code splitting. It hundreds the interactive code as required, when the person calls for it. Bundlers are then in a position to bundle these chunks into bigger bits if it is smart.

Qwik is constructed from the bottom up with three separate capabilities for creating state, template, and listeners. This permits the framework to load solely what’s required for the duty at hand. You possibly can study extra about this chunking side here.

The three boundaries of state, template, and listener had been at one time immediately coded by builders. Because of a brand new Optimizer tool that converts React-like syntax into these boundaries behind the scenes, you get a reasonably acquainted DX. And the Optimizer does the work of turning the precise code right into a set of tiny stubs that may resume the app in small chunks as vital.

With the optimizer, templates and listeners are denoted with a greenback signal, whereas state is dealt with by the useStore hook. You’ll see this syntax in a second.

The ultimate output of Qwik code appears to be like not like that of different frameworks, however utilizing Qwik with the Optimizer brings it into parity with different frameworks. Qwik additionally has launched QwikCity, a set of higher-order options like routing that make it simpler to construct full-scale apps.

Palms-on with Qwik

Now that you’ve an understanding of the ideas behind Qwik, let’s get a really feel for coding with it. Itemizing 1 reveals a easy element written in Qwik. (This instance is from the Qwik FAQ.)

Itemizing 1. Easy Qwik element

import  element$  from '@builder.io/qwik';
export const App = element$(() =>
  console.log('render');
  return <p onClick$=() => console.log('whats up')>Whats up Qwik</p>;
);

Itemizing 1 reveals {that a} element in Qwik is outlined as an nameless perform, handed into the element$ perform from the Qwik library. Any time you see a greenback signal $ in Qwik, it’s letting the Optimizer realize it must do some work. The greenback signed parts of the app are the place Qwik will instrument its fine-grained lazy loading boundaries. 

The onClick$ in Itemizing 1 is one other instance of Qwik’s particular syntax. Qwik will use some trickery to load solely the JavaScript essential to assist the performance when it’s truly required.

The code in Itemizing 1 will probably be damaged up by the Optimizer into a number of segments, as proven in Itemizing 2.

Itemizing 2. Qwik element after compilation

// The app.js file itself
import componentQrl, qrl from "@builder.io/qwik";
const App = /*#__PURE__*/
componentQrl(qrl(()=>import('./app_component_akbu84a8zes.js'), "App_component_AkbU84a8zes"));
export App ;

// app_component_akbu84a8zes.js
import jsx as _jsx from "@builder.io/qwik/jsx-runtime";
import qrl from "@builder.io/qwik";
export const App_component_AkbU84a8zes = ()=>
    console.log('render');
    return /*#__PURE__*/ _jsx("p",
        onClick$: qrl(()=>import("./app_component_p_onclick_01pegc10cpw"), "App_component_p_onClick_01pEgC10cpw"),
        kids: "Whats up Qwik"
    );
;

// app_component_p_onclick_01pegc10cpw.js
export const App_component_p_onClick_01pEgC10cpw = ()=>console.log('whats up');

You possibly can see in Itemizing 2 that as an alternative of together with the precise element performance, Qwik features a reference, utilizing the componentQrl() perform from the library. This perform takes a qrl() perform that makes use of an nameless perform to import the generated element file. This affiliation between elements is all managed beneath the covers by the Optimizer. The developer doesn’t want to consider it immediately.

QRL stands for Qwik URL, which is the best way Qwik references one thing that will probably be lazy loaded. Mainly, any time the framework must defer loading one thing, it should insert a QRL, wrapped by a QRL-specific shopper (like a element, state, or template perform).

For instance, the componentQRL can load on the proper second within the code discovered within the baby element whereas the mother or father can rapidly show its structure. Equally with the onClick handler: It is ready to be evaluated when the press happens.

Qwik CLI

The command line device is on the market from npm and has the fundamental options you’ll count on together with creation, dev mode, and manufacturing construct. The Qwik CLI makes use of Vite because the construct device. You can begin a brand new app with npm create [email protected], which can launch an interactive immediate.

In case you create a easy app and run the manufacturing construct, you’ll get a dist listing the place you possibly can see all the separate lazy-loadable chunks of the app we described earlier.

A Qwik adjustment

An fascinating place to get a way of Qwik syntax is the Qwik Cheat Sheet, which gives side-by-side comparisons of Qwik and React code. You’ll see that total it’s not so laborious a transition. Some areas are fairly comparable, and a few require principally a shift in considering. The larger takeaway is that reactive system in Qwik is radically totally different from React-like frameworks, regardless of the similarity of syntax achieved with the Optimizer. 

Qwik’s revolutionary method to code splitting and lazy loading gives a brand new method ahead for front-end JavaScript. It is going to be fascinating to see the place issues go from right here. 

Copyright © 2022 IDG Communications, Inc.

You might also like

4 Ways to Remove a Specific Item From a JavaScript Array – MUO – MakeUseOf

Toolkit Allows JavaScript Devs to Program Embedded Devices – The New Stack

Select data value from grandparent div? – JavaScript – SitePoint



Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

4 Ways to Remove a Specific Item From a JavaScript Array – MUO – MakeUseOf

by learningcode_x1mckf
March 24, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

4 Ways to Remove a Specific Item From a JavaScript Array  MUO - MakeUseOf Source link

Read more

Toolkit Allows JavaScript Devs to Program Embedded Devices – The New Stack

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Toolkit Allows JavaScript Devs to Program Embedded Devices  The New Stack Source link

Read more

Select data value from grandparent div? – JavaScript – SitePoint

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Select data value from grandparent div? - JavaScript  SitePoint Source link

Read more

How to Handle Errors in JavaScript – Programming – MUO – MakeUseOf

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

How to Handle Errors in JavaScript - Programming  MUO - MakeUseOf Source link

Read more

How to Use the Javascript Slice Method – hackernoon.com

by learningcode_x1mckf
March 23, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

How to Use the Javascript Slice Method  hackernoon.com Source link

Read more
Next Post
Build a Tic-Tac-Toe Game Engine With an AI Player in Python – Real Python

Build a Tic-Tac-Toe Game Engine With an AI Player in Python – Real Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Time limit for notify – JavaScript – SitePoint Forums

Help with closing message box and submiting – JavaScript – SitePoint Forums

December 19, 2022
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

IOException in Java – General and Gameplay Programming – GameDev.net

March 15, 2023
Getting started with SwiftIO – The.Swift.Dev.

Getting started with SwiftIO – The.Swift.Dev.

September 18, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • Developing Multi-Threaded Applications with Java Concurrency API – Data Science Central
  • Gems & Java helps Mothers with a Heart for Ethiopia change lives – Woodstock Sentinel Review
  • 4 Ways to Remove a Specific Item From a JavaScript Array – MUO – MakeUseOf

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?