Just-in-Time (JIT) and Ahead-of-Time (AOT) Compilation in ...

文章推薦指數: 80 %
投票人數:10人

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



請為這篇文章評分?