News for hackers. Stuff that matters.
Re:Why Not Document.write()? by xg15 on Saturday April 01, 2023 @07:56PM (#35405516)
Evaluate the condition server-side and don't transmit the HTML in the first place.
Re:Why Not Document.write()? by remram on Saturday April 01, 2023 @09:01PM (#35405953)
Is that faster though? Can your Ruby/PHP/Python running on the server really do the conditional rendering faster than the JavaScript running in the browser?
Re:Why Not Document.write()? by notRobot on Saturday April 01, 2023 @09:04PM (#35405975)
My experience is mostly with PHP, but the answer to your question is almost always yes.
Re:Why Not Document.write()? by bastawhiz on Saturday April 01, 2023 @10:12PM (#35406423)
Your server is already running a scripting language. Putting a script tag on the page necessarily means the browser then needs to parse and interpret some JavaScript, which it simply wouldn't otherwise need to do. If we assume the server and the client both execute an if statement at the same speed, yes, it's purely adding overhead to run that on the client (not to mention the potential for the additional cost of transmitting the eliminated code).
Re:Why Not Document.write()? by theendisney on Sunday April 02, 2023 @01:09AM (#35407383)
Sorry, I thought it was obvious. The overhead is practically zero compared to the amount of html you can comment out.

You can have a cached static html document with really huge (conditional) comments with tiny performance loss. display:none for example still does all kinds of things. Inserting html with js is even worse.

Re:Why Not Document.write()? by Dalewyn on Sunday April 02, 2023 @02:28AM (#35407753)
You're asking the wrong question.

You should be asking if the Ruby/PHP/Python running on the server can do the conditional rendering cheaper than the JavaShit running in the browser.

Also to answer that question, the answer is: No.

As far as the beancounting is concerned, offload as much of the processing to the client-side and bask yourself in cheaper bills. User needs to wait literal minutes for the page to load? That's none of your concern, they should get a 32-core CPU instead of their 8-core clunker.

Re:Why Not Document.write()? by xigoi on Sunday April 02, 2023 @04:58AM (#35408554)
Don't forget to add a cryptocurrency miner so you can make some money while their CPU is struggling to load the page.
Re:Why Not Document.write()? by xg15 on Sunday April 02, 2023 @05:29PM (#35415450)
Congrats on your convincing use of sarcasm!