Just-in-Time (JIT) and Ahead-of-Time (AOT) Compilation in ...
文章推薦指數: 80 %
An ahead-of-time (AOT) compiler converts your code during the build time before the browser downloads and runs that code. Compiling your ... SigninWRITEFORUS🚀CodingInterviewCourse→Just-in-Time(JIT)andAhead-of-Time(AOT)CompilationinAngularLearnaboutJITandAOT,howtheywork,andacomparisonbetweenJITandAOT.TRANSONHOANGFollowSep2,2019·5minreadPhotobyBrookeLarkonUnsplashInthisarticle,wewillcoverjust-in-timeandahead-of-timecompilation.WewilllookatitinthecontextofanAngularproject,buttheprinciplescanbeappliedtoanyfront-endframework.WhatisJITHowdoesJITworkWhatisAOTHowdoesAOTworkJITandAOTComparisonFormorecontentlikethis,checkouthttps://betterfullstack.comWhatisJITAccordingtoWikipedia:Incomputing,just-in-time(JIT)compilation(alsodynamictranslationorrun-timecompilations)isawayofexecutingcomputercodethatinvolvescompilationduringexecutionofaprogram—atruntime—ratherthanpriortoexecution.Orstatedmoresimply,itisthatthecodegetscompiledwhenitisneeded,notbeforeruntime.HowisJITworkInthebeginning,acompilerwasresponsibleforturningahigh-levellanguageintoobjectcode(machineinstructions),whichwouldthenbelinkedintoanexecutable.Ajust-in-time(JIT)compilerisafeatureoftherun-timeinterpreter,thatinsteadofinterpretingbytecodeeverytimeamethodisinvoked,willcompilethebytecodeintothemachinecodeinstructionsoftherunningmachine,andtheninvokethisobjectcodeinstead.FlowinanAngularProject:UseTypescript,HTML,CSS(SCSSorSASS)todevelopanAngularapplication.Usengbuildtobuildsourcecodeintobundles.Thisincludesassets,JSfiles(modulesinthecaselazyloadandjsmap,vendor,andpolyfill),index.html,andCSS.ThenwebuildthisintoawarfiletodeploybyjbossordeploydirectlybyusingherokuoranotherhostingthatsupportsNode.ThenwemapthishosttoourdomainbyaCNAME.Theend-useraccessesourwebapplicationviathedomain.Thebrowserwilldownloadallassets,includingtheHTML,CSS,andJavaScriptthatisneededforthedefaultview.AngularbootstrapstheapplicationAngularwillgothroughJITcompilationprocessforeachcomponentintheapplication.Thentheapplicationgetsrendered.Note:InJIT,notallthecodeisconvertedintomachinecodeinitially.Onlycodethatisnecessary(usedimmediately)willbeconvertedintomachinecode.Thenifamethodorfunctionalitycalledandisnotinmachinecode,thenthatwillalsobeturnedintomachinecode.ThisreducestheburdenontheCPUandmakestheapprenderfasterbecauseitonlyuseswhatisneeded.YoucandebuginthebrowserduringimplementationbecausethecodewascompiledatJITmodewithamapfile.Thishelpyoucanseeandlinktosourcecodedirectlyoninspector.WhatisAOTAccordingtoWikipedia.Incomputerscience,ahead-of-timecompilation(AOTcompilation)istheactofcompilingahigher-levelprogramminglanguagesuchasCorC++,oranintermediaterepresentationsuchasJavabytecodeor.NETFrameworkCommonIntermediateLanguage(CIL)code,intoanative(system-dependent)machinecodesothattheresultingbinaryfilecanexecutenatively.Thisseemscomplicatedandhardtounderstand.Heishowyoucanthinkaboutit:Anahead-of-time(AOT)compilerconvertsyourcodeduringthebuildtimebeforethebrowserdownloadsandrunsthatcode.Compilingyourapplicationduringthebuildprocessprovidesafasterrenderinginthebrowser.Herearebenefits[2]:Fasterrendering—WithAOT,thebrowserdownloadsapre-compiledversionoftheapplication.Thebrowserloadsexecutablecodesoitcanrendertheapplicationimmediately,withoutwaitingtocompiletheappfirst.Fewerasynchronousrequests—ThecompilerinlinesexternalHTMLtemplatesandCSSstylesheetswithintheapplicationJavaScript,eliminatingseparateAJAXrequestsforthosesourcefiles.SmallerAngularframeworkdownloadsize—There’snoneedtodownloadtheAngularcompileriftheappisalreadycompiled.ThecompilerisroughlyhalfofAngularitself,soomittingitdramaticallyreducestheapplicationpayload.Detecttemplateerrorsearlier—TheAOTcompilerdetectsandreportstemplatebindingerrorsduringthebuildstepbeforeuserscanseethem.Bettersecurity—AOTcompilesHTMLtemplatesandcomponentsintoJavaScriptfileslongbeforetheyareservedtotheclient.Withnotemplatestoreadandnoriskyclient-sideHTMLorJavaScriptevaluation,therearefeweropportunitiesforinjectionattacks.AngularCompilationExplanationbyTobiasBoschHowAOTworksYoucanreadmoreatAngulardocumentationtoseehowAOTworks.But,hereismysimpleexplanation:UseTypescript,HTML,CSS(SCSSorSASS)todevelopanAngularapplication.Usengbuild--prodtobuildsourcecodebundleswhichincludesassets,JSfiles(main,vendor,andpolyfills),index.html,andCSS.Inthisstep,AngularusestheAngularcompilertobuildsourcecodeandtheydoitin3phaseswhicharecodeanalysis,codegeneration,andtemplatetypechecking.Inthisstep,thebundlesizewillbesmallerthanbundlesizewhenwebuildbyJITmode.ThenwebuildthisintowarfiletodeploybyjbossordeploydirectlybyusingherokuoranotherhostingthatsupportNode.ThenwemapthishosttoourdomainusingaCNAME.Theend-useraccessesourwebapplicationviathedomain.ThebrowserwilldownloadallassetstheincludingtheHTML,CSS,andJavaScriptthatisneededforthedefaultview.Angularbootstrapsandtheapplicationgetsrendered.JITandAOTComparisonThemaindifferencesbetweenJITandAOTinAngularare:Just-in-Time(JIT),compilesyourappinthebrowseratruntime.Ahead-of-Time(AOT),compilesyourappatbuildtimeontheserver.JITcompilationisthedefaultwhenyourunthengbuild(buildonly)orngserve(buildandservelocally)CLIcommands.Thisisfordevelopment.ForAOTcompilation,includethe--aotoptionwiththengbuildorngservecommand.Anotherwaysisusing--prodwhichbydefaultproductionmodeisconfiguredinAngular.jsonwithAOTissettotrue.Hereisanothercomparison:JITandAOTcomparisationSummaryJITandAOTaretwowaystocompilecodeinanAngularproject.WeuseJITindevelopmentmodewhileAOTisforproductionmode.WecaneasilyimplementfeaturesanddebuginJITmodesincewehavemapfilewhileAOTdoesnot.However,ThebigbenefitwhenweuseAOTforproductionarereducingbundlesizeforfasterrendering.Ihopeyoufoundthisarticleuseful!YoucanfollowmeonMedium.IamalsoonTwitter.Feelfreetoleaveanyquestionsinthecommentsbelow.I’llbegladtohelpout!Resources/References[1]:JIThttps://guide.freecodecamp.org/computer-science/just-in-time-compilation/[2]:AOThttps://angular.io/guide/aot-compilerStories-BetterFullStackUsefularticlesaboutJavaScript,Python,andWordpressthathelpdevelopersreducetimeindevelopmentandincrease…betterfullstack.comLevelUpCodingCodingtutorialsandnews.Follow5782JavaScriptAngularProgrammingFrontEndDevelopmentCoding578 claps5782WrittenbyTRANSONHOANGFollowSeniorJavaScriptEngineeringFollowLevelUpCodingFollowCodingtutorialsandnews.Thedeveloperhomepagegitconnected.com&&skilled.devFollowWrittenbyTRANSONHOANGFollowSeniorJavaScriptEngineeringLevelUpCodingFollowCodingtutorialsandnews.Thedeveloperhomepagegitconnected.com&&skilled.devMoreFromMediumHackathon#HackZurich:whatusefulsolutioncanyoucodefordriversin40hours?AlexanderDimchenkoinBrightBox — DrivingtothefutureImplementationofUUIDandbinary(16)ingormv2.DipeshK.C.inwesionaryTEAMJVMArchitechtureanditsWorking.dishasaxenaImportantDockercommandsforBeginnersLinuxTechLabAlternativeWaytoPerformORQueryinCloudFirestoreMendhieEmmanuelinTheStartupSecondDayofHackReactorAlexBarclayrandomblogoccurringontherightday.happy[any]day.DavidG.HorsmanNuxtjs+Prismic+OneSignal=AwesomePWACarlHandy
延伸文章資訊
- 1Ahead-of-time compilation - Wikipedia
In computer science, ahead-of-time compilation (AOT compilation) is the act of compiling an (ofte...
- 2Ahead-of-time (AOT) compilation - Angular
The Angular ahead-of-time (AOT) compiler converts your Angular HTML and TypeScript code into effi...
- 3Just-in-Time (JIT) and Ahead-of-Time (AOT) Compilation in ...
An ahead-of-time (AOT) compiler converts your code during the build time before the browser downl...
- 4Using the Ahead-of-Time (AOT) Compiler | Pluralsight
The Ahead-of-Time (AOT) Compiler ... We can decrease the size of the build and compile it at the ...
- 5Angular 4 教學- Webpack 預先編譯Ahead-of-Time (AOT)
Ahead-of-Time (AOT) 預先編譯,是在程式發佈之前就透過Angular Compiler 進行編譯,所以瀏覽器下載完的 *.js 檔案,就可以直接被執行,然後渲染畫面 ...