![]() |
|
Re: Learning Javascript Thread
"It's because there's no purpose to it and it just splits people apart, that is why the JS community who doesn't use CS is against it."
I'm picturing a unified world of assembly programmers. Nothing adds capabilities to assembly. |
Re: Learning Javascript Thread
Quote:
The v8 engine (Google's JS engine) compiles JS directly into asm. Why write that by hand when a machine can likely write better asm than most humans? Surely you're not comparing writing JS to asm and thinking people are being stubborn by not using something else to write JS? |
Re: Learning Javascript Thread
You know, recently someone did say "JavaScript is the Assembly language of the web." :P
|
Re: Learning Javascript Thread
interesting discussion. my interest in coffeescript is from a long-term code maintainability concern. javascript is not yet that cumbersome in my modest codebase.
|
Re: Learning Javascript Thread
Quote:
|
Re: Learning Javascript Thread
Quote:
Quote:
I'd also say at the top JS has the higher skill level people. I mean your argument is "more people use it" which I don't think is a particularly good argument. The tough gotchas require JS skill anyways is a pretty good argument for learning JS though. (obviously way off topic-ish but an interesting debate nontheless imo) |
Re: Learning Javascript Thread
People working on CS would use it, yes. I was talking about working on open source projects and contributing to open source projects. You can't just say "oh get a coffee to JS compiler and then read the JS!", because the JS it outputs is ridiculous. Also that's an unreasonable thing to ask someone to do in the first place.
I only brought stats in to show that it's not wildly popular. Those were the first 2 sites that popped into my head to get stats from. |
Re: Learning Javascript Thread
Quote:
Out of curiosity, would you consider Clojurescript a legitimate switch? I'm still just dipping my toes into the clojure world, but I'd probably be moving that way a lot quicker if it weren't for coffeescript. If not clojurescript, then what would constitute the addition of real capabilities? |
Re: Learning Javascript Thread
Quote:
jtollison basically hit the nail on the head. I'm pretty much done with this debate as I've lived through these debates with people so much over the last year. It really doesn't bother me that not everyone embraces CS. |
Re: Learning Javascript Thread
Didn't even know there was Clojurescript...that seems to be something to add onto my list of cool stuff to check out (below Clojure I guess heh) :)
|
Re: Learning Javascript Thread
It doesn't bother me. You guys just came into the thread trying to convince someone new to JS to immediately switch without listing the downsides or even giving a single concrete reason to switch.
|
Re: Learning Javascript Thread
Quote:
Clojurescript doesn't seem quite as compelling to me as clojure itself, but still worthwhile. |
Re: Learning Javascript Thread
wtf, seriously? Can I not link to free instructional videos here? I know I don't post enough to be up on the rules, but... blip dot tv slash clojure = lots of good clojure videos including the clojurescript introduction from Rich Hickey.
edit: I immediately followed up here with the address partly out of frustration, but partly assuming this link wouldn't actually be breaking any rules. Maybe it is. I'm sure someone will take it down if that's the case. If this is a violation of some rules policy, it seems like a bad policy to me and, fwiw, will motivate me to spend less time here. I don't like feeling like I'm in prison while visiting programming forums. |
Re: Learning Javascript Thread
Before Reading, note the difference: Clojure ; Closure.
I love Lisp and I love Clojure, but I'm not loving ClojureScript. The reason is that ClojureScript compiles down to Closure, not bona fide javaScript. Closure is Google's JS framework/boilerplate/whatever, which in turn compiles down to fast-compiling JS. The issue with Closure, as I understand it, is that Closure was created to solve Google's problems, not the world's problems per se, thus, to get Closure to work well, you have to dip into the JS or use ordinary JS. So, with ClojureScript, you are dealing with a sort of pre-processor to a pre-processor, and you have to understand not only the limitations of Clojure --> JS, but also the limitations of Clojure --> Closure, and Closure --> JS. Obviously the big one is taking a programming language with concurrent / streaming built-in and converting that to single-line applications. Then there's apparently some key words that ClojureScript decided to remove from JS. Not sure how important this is, but apparently eval() is the one everyone fusses about. Anyways, I'm not saying it isn't viable, but that I'm not too plussed by it yet. Of course, I'm not a JS maestro either. Now, as far as back-end programming: as someone who isn't a maetro on that subject, I think Clojure is amazing. |
Re: Learning Javascript Thread
Quote:
|
Re: Learning Javascript Thread
Code:
function buildList(list) {Code:
function buildList(list) { |
Re: Learning Javascript Thread
e i pi,
Your code does work, but I think there may be a better way to do it. If you JSHint your code, it will return an error that says "Don't make functions in a loop." Here is an outstanding explanation of that error. A basic rule to follow is that if a function is only supposed to execute once (like on page load), an immediately invoked function expression is the way to go. If the function is needed multiple times, there is a performance penalty for running the IIFE multiple times (like in a loop). For that, function declarations are better. Here's my rewrite: Code:
function testList(arr) {Also, it's generally a good idea to declare variables at the top, especially if you're calculating a value that will be used in a loop. In your code, you have this: Code:
for (var i = 0; i < list.length; i++) {Code:
var size = list.length, |
Re: Learning Javascript Thread
Quote:
The real reason not to generate closures inside of a loop has to do with what the previous poster said - loops (or any javascript blocks for that matter) don't create a new scope, so unless you specifically create another level of scope (which his fix does with functions), you're creating the same closure over and over again, which is almost certainly not what's intended. Quote:
As for how to cleanly rewrite the original code, this should work: Code:
function buildList(arr) { |
Re: Learning Javascript Thread
Quote:
Quote:
More to the point, he never asked that. He wanted to hear opinions on the quality of his code. I pointed out the JSHint error, which actually is related to coding practices -- his code still works. The SO answer I pointed to is in response to precisely that JSHint message and mentions variable scope, performance, provides alternate solutions, and so on. Furthermore, it's been upvoted 25 times. Anyone on SO for more than 5 minutes understands what that means. Quote:
Quote:
Quote:
For Internet Explorer -- the most important performance consideration -- benchmarks show that performance enhancements of up to 244% are achievable by caching the array length. See for yourself: http://jsperf.com/caching-array-length/4 Yes, obviously if that's the only thing our JS code does, then it isn't going to matter. But for applications that rely heavily on highly complex JS code, in the aggregate, it can and does matter. Especially for older versions of IE for users with slow computers. Also, the primary reason for declaring variables at the top is for code readability and organization. I honestly thought that was too obvious to point out, and decided to offer a less apparent consideration instead. Clearly, I was mistaken. Quote:
Btw, you're missing some semicolons. |
Re: Learning Javascript Thread
Quote:
See: https://groups.google.com/forum/?fro...rs/BbvL5qFG_uc Quote:
Of course the readability suffers - extra lines of boilerplate are going to add up. This isn't the only place you're going to cache, is it? If you make the equivalent caching decisions for all potentially slow operations in javascript, it's going to add up tremendously. It's extremely rare to be writing performance-critical code in any language and even rarer in javascript. And if you're going to care about performance, you don't create closures this way. You'd preallocate arrays and use them for most things and try hard to get the compiler not to box your primitives and try your hardest to never allow any objects to be created dynamically. Unless you're doing this and using javascript essentially as C, it's very likely that something else you're doing will be so slow to make this optimization entirely meaningless. And theoretically, it's easier to optimize the original code. What's worse, I think in many cases, the extra bytes transmitted cost more than the time you save in the loop. Quote:
Quote:
The chance is, you've probably never written code where this performance difference ever mattered. This is so cheap compared to rampant object/closure creation and ajax calls that go on in javascript that it's not worth thinking about, unless you're writing the inner most loop in some framework/libraries that are going to be heavily used. Even then, this is dubious. Profilers will tell you where you need to optimize. Quote:
https://gist.github.com/2841832 So that you can design for performance. Quote:
Quote:
Quote:
|
Re: Learning Javascript Thread
Quote:
1) You use arr.length before you check whether arr is an array, rendering the check largely worthless. 2) Using .push to append to an array when you know the exact length is much worse than not caching .length. You want to preallocate the array and populate as you go. 3) Your check is worse than not having it in the first place in most cases. If you don't want it to blow up, return an empty array. If you want it to blow up, then there's no need for a check. The caller can just as easily check his argument type as he can check the return type. Overall, your code is slow, much slower than mine, despite your "caching." And I ran some performance tests and my code is about twice as fast on chrome23 (50 seconds versus 100 seconds) and almost infinitely faster on IE10 (7 seconds versus it's been a few minutes and it still hasn't finished probably because it's running into some memory issues). This is after removing your check for an apples-to-apples comparison. Code:
function buildBigArray(n) { |
Re: Learning Javascript Thread
Quote:
Here's some friendly advice, though -- You don't earn any credibility by making claims about your knowledge and experience. No one read that you've written a compiler and decided to give you more credibility. Particularly on 2p2, credentials mean nothing; actual evidence of ability means everything. Quote:
You're also arguing a strawman. At no point did I say anything about caching in any other situation than this one specific scenario. You're putting words in my mouth and then lecturing me on why they're wrong. Quote:
And here is your response: The irony that I was pointing out is that you dismissed an earlier point as semantics, and then later argued that I was wrong because I used the term "calculated" when in fact the length value is a stored property and doesn't need to be actually "calculated". It's a gem b/c you didn't realize you were arguing semantics, when you clearly were, in the same post where you accused me of arguing semantics (which, btw, I was not). Here's how you can spot an argument based on semantics: If you're contradicting the principle of another's argument based on their use of similar but not 100% technically accurate terms, and the principle of the argument remains even if the terms are swapped, you're doing it wrong. Quote:
Quote:
|
Re: Learning Javascript Thread
Quote:
Quote:
Quote:
http://jsperf.com/pre-allocated-arrays http://stackoverflow.com/a/1295671/1091949 Quote:
Quote:
|
Re: Learning Javascript Thread
Quote:
https://groups.google.com/forum/?fro...rs/BbvL5qFG_uc Otherwise, you should submit a pull request here: http://jslinterrors.com/dont-make-fu...within-a-loop/ Quote:
Quote:
Btw, your solution is also slower than the original in Chrome, marginally faster in IE9. Yeah, I'm sure that's what JSLint/Hint was warning you about. They're really worried that you may write code that runs slightly faster in Chrome but slightly slower in IE. Quote:
This has nothing do with this argument here, where you used the term "calculated" as though it's a costly operation. It's a lookup. The reason this distinction matters is that there data structures where this property must be calculated and in those cases you'd be advised to cache the result. This is not semantics, this is you trying to get away with incorrect inference (calculation -> must be expensive -> must cache). If it's a stored property, the argument for caching becomes quite thin. If it's not an expensive calculation, why are you caching it? Do you routinely cache references to object properties? Quote:
|
Re: Learning Javascript Thread
Quote:
Quote:
Quote:
Quote:
Quote:
|
| All times are GMT -4. The time now is 05:48 PM. |
|
Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Copyright © 2008-2020, Two Plus Two Interactive