Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MacBH928

macrumors G3
Original poster
May 17, 2008
8,327
3,719
I have no idea about programing but I stumbled about a website called Nitter.net that is front-end(I guess) for Twitter and I was surprised how fast and responsive it is. Intrigued I went to GitHub and discovered its mostly made by Nim programming language.

I don't know what it is, why its fast, but if Nim is this fast all websites should be programmed in Nim! I am sure it has its limits but you get the idea. Why its not more popular?
 
Last edited:

casperes1996

macrumors 604
Jan 26, 2014
7,425
5,545
Horsens, Denmark
Your web browser will interpret HTML, JavaScript (and WebAssembly/WebGL/WebGPU but that's a long story to get into).
Nim is a cool language, but for web use it's transpiled into JavaScript (may be a WebAssembly transpile option these days, dunno).

There's no magic to it being done with Nim. You can achieve the same speed writing your own JavaScript (exclusively talking about web here), TypeScript or any of a myriad of other languages. Nim may have a good optimiser backend for its JavaScript transpiler, but I assure you the same results can be achieved with another language.

Doing a linear search over a billion element array is slow compared to doing a binary search; What language you do it in is irrelevant. That's of the bigger significance here.
 

MacBH928

macrumors G3
Original poster
May 17, 2008
8,327
3,719
So if the end result it just outputs JavaScript code, how come its so much faster than all other websites?
Why is someone bothering to create a language that put out in another language? Why not just program in the original language in the first place?

Doing a linear search over a billion element array is slow compared to doing a binary search; What language you do it in is irrelevant. That's of the bigger significance here.

I have no idea what you are saying
 

casperes1996

macrumors 604
Jan 26, 2014
7,425
5,545
Horsens, Denmark
So if the end result it just outputs JavaScript code, how come its so much faster than all other websites?
Why is someone bothering to create a language that put out in another language? Why not just program in the original language in the first place?
Why bother with programming languages at all when we could just write out all the instructions in binary to begin with?

Modern compilers (the programs that transform source code from one language into another form, like machine code) have generally been built around LLVM - Whether this is also the case for Nim I haven't looked into, but I would assume so.

This means that when you're developing a new language, all you do is create a "front-end" that takes your language and spits out LLVM-IR. Then you can pass that on to an LLVM-backend that can then spit out the appropriate output, whether that be x86_64, AArch64 (ARM), WebAssembly, JavaScript, PPC, whatever you want.

The advantage here to just writing JavaScript is familiarity and choice. The developer may have been more familiar with or preferred Nim, and since it can output in the desired format that was the language of choice.


Also, when I go to "Nitter.com" what I see does not look like what you describe. I get a design company (that is not using SSL).

But as for speed; There can be a myriad of reasons for that, but:
I have no idea what you are saying

What I was saying is that the code you write matters more than the language it's written in
 
  • Like
Reactions: MacBH928

MacBH928

macrumors G3
Original poster
May 17, 2008
8,327
3,719
Why bother with programming languages at all when we could just write out all the instructions in binary to begin with?

Modern compilers (the programs that transform source code from one language into another form, like machine code) have generally been built around LLVM - Whether this is also the case for Nim I haven't looked into, but I would assume so.

This means that when you're developing a new language, all you do is create a "front-end" that takes your language and spits out LLVM-IR. Then you can pass that on to an LLVM-backend that can then spit out the appropriate output, whether that be x86_64, AArch64 (ARM), WebAssembly, JavaScript, PPC, whatever you want.

The advantage here to just writing JavaScript is familiarity and choice. The developer may have been more familiar with or preferred Nim, and since it can output in the desired format that was the language of choice.


Also, when I go to "Nitter.com" what I see does not look like what you describe. I get a design company (that is not using SSL).

But as for speed; There can be a myriad of reasons for that, but:


What I was saying is that the code you write matters more than the language it's written in

Sorry, its nitter.net. You put in a twitter usename and it shows you the tweets. This way you don't have to create a twitter account.

So the work goes like this NIM--compiler--> Javascript ? Or NIM--compiler-->C++--compiler--> app ? I don't know too much but I remember things like this don't translate well. Maybe things got advanced.

What I was saying is that the code you write matters more than the language it's written in

I thought each language has advantages and disadvantages like one is faster, the other can processes more info, one can do graphics...etc So choice of language pretty much matters. Like I don't know if you can make an operating system with JavaScript or build a cite in C++.

thanks for taking the time to answer my questions.
 

casperes1996

macrumors 604
Jan 26, 2014
7,425
5,545
Horsens, Denmark
Sorry, its nitter.net. You put in a twitter usename and it shows you the tweets. This way you don't have to create a twitter account.

So the work goes like this NIM--compiler--> Javascript ? Or NIM--compiler-->C++--compiler--> app ? I don't know too much but I remember things like this don't translate well. Maybe things got advanced.

I tried out the page. Honestly it's probably about the speed I'd expect if you take away all the extra fluff from a site like Twitter.

So the process for most (not all) modern compilers is
Source Language -> Compiler Front-end -> LLVM-IR -> Compiler backend -> Target language

Almost regardless of target. Target language including machine code instructions as a binary executable or in this case a web

Upon a brief inspection however it doesn't look like there's JS at all actually. At least on the front-page which is all I really inspected. What it looks like to me is that they have a server-side program that reacts to GET requests and sends back appropriate HTML.
So the majority of this site would be a normal application binary running on their server and the web interface is just a small view into that.
But I've only very briefly looked it over.

I thought each language has advantages and disadvantages like one is faster, the other can processes more info, one can do graphics...etc So choice of language pretty much matters. Like I don't know if you can make an operating system with JavaScript or build a cite in C++.

thanks for taking the time to answer my questions.

… Sort of. It's complicated, hehe.

As you probably know, Java traditionally runs on the Java Virtual Machine. But there's technically nothing preventing someone from making a compiler for the Java language that compiles to pure machine code. I don't know if this has ever been done for Java specifically but the language Kotlin, which also runs on the Java Virtual Machine, has a project called Kotlin Native to achieve exactly this.

Do different languages have strengths and weaknesses? Yes, you could absolutely say that. But it's not always as straight forward as language A fast, B uses less memory, C better for web or whatever. We can really adapt pretty much any tool to any job.

And I mean your understanding isn't completely wrong or anything.
Write the exact same program in C and Java (they're similar enough in syntax and to an extend semantics that this can be possible, class structure aside) and C will generally be faster because you don't need the JVM.
Left with the choice of competent Java or incompetent C though, Java will run faster regardless though. In my original post I mentioned linear search and binary search; Let me elaborate a bit on that.

Let's say you're looking for the number 42 in a list of 10 numbers. You know the list is sorted lowest to highest but you have no idea of the range of numbers. Could be: 1,2,3,4,10,11,14,42… or it could be 42,50,100, 20 million… and so on. You don't know. How do you find the number? A linear search would be just going through the list one number at a time. Thus the time complexity; I.e the worst case time to finish, is O(N) where N is the length of the list. That is a time to finish that linearly scales with the number of elements to look over.

A more efficient approach is to start with the middle number; The fifth element. And say "Are we looking for a bigger or smaller number?" Then take the middle number in that direction; So if 42 is smaller than the fifth element, we'd look at nr. 3. If it's still smaller we just look at 1 and 2, if it's bigger the number is in position 4.
With this way of searching our time complexity is O(log(n)) or in other words the time to finish scales logarithmically with the number of elements in the list; Every time we look, we cut the search space in half.

On a big enough dataset, the binary search method will always be faster than the linear search regardless of language or runtime.

Which brings us neatly to our next complication; Runtimes. Because a language also has a runtime component. A language in itself is not really fast or slow, but the way it compiles and runs can be. The runtime is all the things for a given execution context that are not handled ahead of time often considered a core part of the language regardless but in a way is a separate program that runs alongside programs in a given language.
Runtimes are often related to what people consider the speed of a language, and in some cases are unavoidable as well if you want to meaningfully use a given language but in other cases your reliance on the runtime is a choice. (to an extent)

And last but not least I just want to touch upon the "speed on what hardware" issue as well. Most C compiler for example can highly optimise for a specific micro-architecture. Like way beyond just x86_64, but all the way down to like "Haswell chip" or whatever. And that's fantastic but could result in terrible performance as well on another target, even if it's the same ISA, x86_64. This would of course be unlikely and require tremendous modifications to the relative speed of certain instructions, but theoretically is possible.
An interpreted environment like JavaScript usually runs in will be capable of tailoring the code to your hardware every single time you run it. The results may not be as good today, but without ever recompiling it, an update to the interpreter can improve the speed.

And in the end, as mentioned, any language can theoretically be made to do anything. Whether the tooling exists to easily achieve that at the moment I can't guarantee, but it's possible. So in short, the code you write matters more than the language it's written in :)
 
  • Like
Reactions: MacBH928

MacBH928

macrumors G3
Original poster
May 17, 2008
8,327
3,719
I tried out the page. Honestly it's probably about the speed I'd expect if you take away all the extra fluff from a site like Twitter.

So the process for most (not all) modern compilers is
Source Language -> Compiler Front-end -> LLVM-IR -> Compiler backend -> Target language

Almost regardless of target. Target language including machine code instructions as a binary executable or in this case a web

Upon a brief inspection however it doesn't look like there's JS at all actually. At least on the front-page which is all I really inspected. What it looks like to me is that they have a server-side program that reacts to GET requests and sends back appropriate HTML.
So the majority of this site would be a normal application binary running on their server and the web interface is just a small view into that.
But I've only very briefly looked it over.



… Sort of. It's complicated, hehe.

As you probably know, Java traditionally runs on the Java Virtual Machine. But there's technically nothing preventing someone from making a compiler for the Java language that compiles to pure machine code. I don't know if this has ever been done for Java specifically but the language Kotlin, which also runs on the Java Virtual Machine, has a project called Kotlin Native to achieve exactly this.

Do different languages have strengths and weaknesses? Yes, you could absolutely say that. But it's not always as straight forward as language A fast, B uses less memory, C better for web or whatever. We can really adapt pretty much any tool to any job.

And I mean your understanding isn't completely wrong or anything.
Write the exact same program in C and Java (they're similar enough in syntax and to an extend semantics that this can be possible, class structure aside) and C will generally be faster because you don't need the JVM.
Left with the choice of competent Java or incompetent C though, Java will run faster regardless though. In my original post I mentioned linear search and binary search; Let me elaborate a bit on that.

Let's say you're looking for the number 42 in a list of 10 numbers. You know the list is sorted lowest to highest but you have no idea of the range of numbers. Could be: 1,2,3,4,10,11,14,42… or it could be 42,50,100, 20 million… and so on. You don't know. How do you find the number? A linear search would be just going through the list one number at a time. Thus the time complexity; I.e the worst case time to finish, is O(N) where N is the length of the list. That is a time to finish that linearly scales with the number of elements to look over.

A more efficient approach is to start with the middle number; The fifth element. And say "Are we looking for a bigger or smaller number?" Then take the middle number in that direction; So if 42 is smaller than the fifth element, we'd look at nr. 3. If it's still smaller we just look at 1 and 2, if it's bigger the number is in position 4.
With this way of searching our time complexity is O(log(n)) or in other words the time to finish scales logarithmically with the number of elements in the list; Every time we look, we cut the search space in half.

On a big enough dataset, the binary search method will always be faster than the linear search regardless of language or runtime.

Which brings us neatly to our next complication; Runtimes. Because a language also has a runtime component. A language in itself is not really fast or slow, but the way it compiles and runs can be. The runtime is all the things for a given execution context that are not handled ahead of time often considered a core part of the language regardless but in a way is a separate program that runs alongside programs in a given language.
Runtimes are often related to what people consider the speed of a language, and in some cases are unavoidable as well if you want to meaningfully use a given language but in other cases your reliance on the runtime is a choice. (to an extent)

And last but not least I just want to touch upon the "speed on what hardware" issue as well. Most C compiler for example can highly optimise for a specific micro-architecture. Like way beyond just x86_64, but all the way down to like "Haswell chip" or whatever. And that's fantastic but could result in terrible performance as well on another target, even if it's the same ISA, x86_64. This would of course be unlikely and require tremendous modifications to the relative speed of certain instructions, but theoretically is possible.
An interpreted environment like JavaScript usually runs in will be capable of tailoring the code to your hardware every single time you run it. The results may not be as good today, but without ever recompiling it, an update to the interpreter can improve the speed.

And in the end, as mentioned, any language can theoretically be made to do anything. Whether the tooling exists to easily achieve that at the moment I can't guarantee, but it's possible. So in short, the code you write matters more than the language it's written in :)

Thank you this have been very enlightening and I learned something.
I guess I should rephrase my title: Every website should be built with the same methodology Nitter.net was built
 

casperes1996

macrumors 604
Jan 26, 2014
7,425
5,545
Horsens, Denmark
Thank you this have been very enlightening and I learned something.
I guess I should rephrase my title: Every website should be built with the same methodology Nitter.net was built
You're welcome! It's been fun to write out, and relevant since I just had my exam in compilers today, hehe. So I've been through making all of the code to translate from a language into LLVM and further to x86_64.
Anyhoot, on the point of the web, yeah a lot of websites are bloated and inefficient. Part of this is the fault of the advertising economy and a million attempts at tracking people, part of it is the historic evolution of the web, and part of it is just prevalence of inefficient practices in the web space
 
  • Like
Reactions: MacBH928
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.